0

I wrote the following code, which opens a .txt file and writes its content to the console, but it shows me a NullPointerException, could you help me on debugging this?

box = new JFileChooser();

returnVal = box.showOpenDialog(null);
if (returnVal == box.APPROVE_OPTION) {
    File file = box.getSelectedFile();
}
try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    while (br.readLine() == null) {
        System.out.println(br.readLine());
    }
} catch (Exception e1) {
    e1.printStackTrace();
}
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Kaleab
  • 367
  • 1
  • 4
  • 13
  • 1
    `while (br.readLine() == null)`?? Shouldn't it be... `!= null`? – AJPerez Dec 07 '14 at 13:30
  • @AJPerez correct, also it should be stored in variable, or a line will be skipped, i have already added a full answer :) – Yazan Dec 07 '14 at 13:32
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Erwin Bolwidt Dec 07 '14 at 14:20

2 Answers2

2

The file variable that you initialize only exists inside the scope of the if statement. You must be using a different file variable in the try block, which is probably not initialized (if you don't have another file variable declared in code you didn't include in the question, your code can't pass compilation).

Change

        if (returnVal == box.APPROVE_OPTION){
            File file = box.getSelectedFile();
        }

to

        if (returnVal == box.APPROVE_OPTION){
            file = box.getSelectedFile();
        }
Eran
  • 387,369
  • 54
  • 702
  • 768
  • I feel like you should also add in the fact that the try-catch statement should, itself, be inside the if-statement. – T-Fowl Dec 07 '14 at 13:25
  • @T-Fowl That's one option, but it doesn't have to be done this way. Perhaps the `file` variable is used in other places we don't see here, in which case it shouldn't be declared inside the if statement, only initialized. If it's declared outside the if statement, the try-catch can stay outside the if statement. – Eran Dec 07 '14 at 13:27
  • That is true, I did not think of this that way. – T-Fowl Dec 07 '14 at 13:31
  • The nullPointerexeption is not showig at this time , But doesn't print any thing , and not stop running. – Kaleab Dec 07 '14 at 13:33
2

i think this code have more than 1 mistake find changes 1-5 CHANGE 1 CHANGE 2 CHANGE 3 CHANGE 4 CHANGE 5

box = new JFileChooser();
File selectedFile = null; // CHANGE 1

returnVal = box.showOpenDialog(null);
if (returnVal == box.APPROVE_OPTION) {
    selectedFile = box.getSelectedFile(); // CHANGE 2
}
try {
    BufferedReader br = new BufferedReader(new FileReader(selectedFile ));//CHANGE 5
    String line = "";
    while ((line=br.readLine()) != null) {//CHANGE 3
        System.out.println(line);//CHANGE 4
    }

    br.close();//@Tom comment:
} catch (Exception e1) {
    e1.printStackTrace();
}
Yazan
  • 6,074
  • 1
  • 19
  • 33
  • EDIT: NOW 5 CHANGES:: i recommend change variable name from `file` to `selectedFile` as `file` looks to be used and declared somewhere else, thats why you got null. – Yazan Dec 07 '14 at 13:35
  • Change 4 and Change 5 was best solution , then it works with out any changes. – Kaleab Dec 07 '14 at 14:03
  • 1
    There is another issue: the `BufferedReader` remains open. – Tom Dec 07 '14 at 14:25