Reputation: 1065
So I'm currently getting a null pointer on the last line of this method. From everything I've read on others with this issue, using the while ((lineIn = myReader.readLine()) != null)
should be stopping the file once it has nothing left, but it doesn't seem to do that. Instead I end up catching a NPE. I can't figure out exactly why that is. Any advice would be appreciated! I currently band-aid the issue by using another catch (NullPointerException)
statement, but I don't feel that that's an adequate solution.
BufferedReader myReader;
try {
FileInputStream fileInStream = new FileInputStream(
fileLocation);
InputStreamReader fileInputStreamReader = new InputStreamReader(
fileInStream, "UTF-16");
myReader = new BufferedReader(fileInputStreamReader);
String lineIn = "";
// Read the next line until there aren't any left
while ((lineIn = myReader.readLine()) != null) {
//Do stuff with line
}
System.out.println("done");
// Close the file connections now that the read is done.
fileIn.close();
myReader.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
Upvotes: 0
Views: 185
Reputation: 2300
Null Pointer should never be caught it should be fixed. You do not want them to propagate to the remaining parts of your code.
Also ensure you are closing your file. Add a finally block and close those files resources. This way if an exception is caught you will still close your file.
Upvotes: 3
Reputation: 1065
Turns out the issue was I hadn't properly closed my file readers from previous operations. Thank you guys.
Upvotes: 0