2

I'm trying to open a directory get its name and load all the images from it. I'm getting a null pointer after the first system out print. What am I doing wrong? Is there also a better way to code this?

Here is my code:

public void open() {
    JFileChooser chooser = new JFileChooser();
    File studyPath = new File("C:\\");

    chooser.setCurrentDirectory(studyPath); 
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    int returnVal = chooser.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) { 

        // gets study path and sets study name
        studyPath = chooser.getSelectedFile();
        studyName = chooser.getName(studyPath);

        // get study images
        int x;
        int y;
        for(File i : studyPath.listFiles()) {
            try {
                FileInputStream file_input_stream = new FileInputStream(i);
                BufferedImage image_file = ImageIO.read(file_input_stream);

                x = image_file.getHeight();
                y = image_file.getWidth();

                int[] res = {x, y};

                StudyImage image = new StudyImage(i.getName(), res, image_file);
                System.out.println(image.toString());
                studyImages.add(image);  

            } catch (FileNotFoundException ex) {
                Logger.getLogger(Study.class.getName()).log(Level.SEVERE, 
                            null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Study.class.getName()).log(Level.SEVERE, 
                            null, ex);
            }  
        }

    }
}

But I get a null pointer after the first Sys out print line

Stack trace:

run:
Image name: ct_head01.jpg    Resolution: [I@5f85f4b7
Exception in thread "main" java.lang.NullPointerException
    at medicalimageviewer.Study.open(Study.java:69)
    at medicalimageviewer.MedicalImageViewer.main(MedicalImageViewer.java:12)
Java Result: 1

What studyImage is:

private LinkedList<StudyImage> studyImages;
7
  • 3
    Where does studyImages come from? StudyImages, with a capital s, is not the same. studyImages is null, which is why you are getting the NPX. We need more information (like the code where studyImages comes from) before we can actually help you. Commented Feb 21, 2014 at 4:20
  • 2
    Can you show us the line where you initialise the studyImages variable please? Commented Feb 21, 2014 at 4:20
  • 2
    More important than your specific NullPointerException (or NPE) is to understand the process of debugging NPE's. You must check the variables that you dereference on the line that throws the NPE and see where you don't initialize them, when you think you do. If you did that, you'd at least know to show us where you think you initialize the studyImages variable, instead of making us beg for the code. Commented Feb 21, 2014 at 4:22
  • 2
    Yep, you never initialize it. Commented Feb 21, 2014 at 4:23
  • 2
    By the way, listFiles() may give a folder too, and you're treating all elements as image files Commented Feb 21, 2014 at 4:23

3 Answers 3

3

Even though you didn't include it in your post, studyImages must not be initialized (studyImages is null).

// I suggest you use the interface type.
private List<StudyImage> studyImages = new LinkedList<StudyImage>();
// on java 7 and up, you could do
// private List<StudyImage> studyImages = new LinkedList<>();
// or
// private List<StudyImage> studyImages = new ArrayList<>();
Sign up to request clarification or add additional context in comments.

2 Comments

Ah this could be it let me check!
Accept in my case its new LinkedList<StudyImage>(). Thank you!
3

It's hard to tell as the rest of your class is missing, but it could be that your studyImages variable is not initialized when you called it.

My advice is that it is best to initialize a variable right after you have declared it (there are some exceptions to this, such as initializing variables in a constructor), so if it is stored as a field, I would write: List<StudyImage> studyImages = new ArrayList<StudyImage>(); if you have not already done this.

Comments

2
private LinkedList<StudyImage> studyImages;

is nothing. This is the "declaration" which is just a pointer to where studyImages will go. It must be "initialized", as suggested by @Elliot Frisch. When you are calling

studyImages.add(image);  

it's crashing with a NullPointerException because you are attempting to call a function on an object that does not exist.

Here's some more information:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.