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;
studyImagescome from?StudyImages, with a capital s, is not the same.studyImagesis null, which is why you are getting the NPX. We need more information (like the code wherestudyImagescomes from) before we can actually help you.studyImagesvariable please?studyImagesvariable, instead of making us beg for the code.listFiles()may give a folder too, and you're treating all elements as image files