Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This is the code I working on. It's inside a method and the idea is for it to open a file and check the contents, or the lack of, and report back.

However I'm getting a NullPointerException on the line that I pointed to below.

I have no idea how to fix this. I tried debugging which shows that at the time the line is run the 1st element of the String[] contains text, so that's not a problem.

int i = 0;
int numChar=1, numLines;
String[] line = new String[1000];

try {
    BufferedReader in = new BufferedReader(new FileReader(file));
    try {
        while(numChar > 0) {
            //String[] line = new String[1000];
            line[i] = in.readLine();
PROBLEM-->  numChar = line[1].length();
            i++;
        }            
    } catch (EOFException ex) {
        JOptionPane.showMessageDialog( null, "Error" );
        //break;
    }      
}
catch(IOException e) {
    JOptionPane.showMessageDialog( null, "Missing file or no data to read." );
    System.out.println("IO Error - Missing file");
}   
share|improve this question
up vote 2 down vote accepted

I suspect you just need to change the array access index to use i instead of 1.

numChar = line[i].length();

And you should also check for null as the BufferedReader will return (from the docs):

null if the end of the stream has been reached

numChar = line[i] == null ? 0 : line[i].length;

You might want to expand that so you break out of your loop instead of assigning a length of null.

String s = in.readLine();
if (s == null) {
  break;
}
else {
  line[i] = s;
  numChar = line[i++].length();
}

EDIT in response to comments.

At risk of confusing the issue, my preference would be to rewrite your loop. You don't seem to need numChars outside the loop, so I would remove it so as to reduce your method scope's variables. I also suspect you don't want to stop reading on empty lines, just at the end of the stream:

while (true) { // for(;;) if you prefer
    String s = in.readLine();
    //if (s == null || s.length() == 0) break; // stop on empty lines and end of stream
    if (s == null) break; // stop at end of stream only
    line[i++] = s;
}
share|improve this answer
    
That's my bad, it used to be an 'i' but I changed it to '1' while trying to debug, it gives same error when it's i – user2627736 Jan 31 '15 at 14:19
    
@user2627736. Yes, you'll need to do a null check as well - see my edit with the documentation reference. – Andy Brown Jan 31 '15 at 14:25
    
Imho it will be more efficient to rewrite the loop condition instead of if else logic. – RandomHuman Jan 31 '15 at 14:26
    
@RandomHuman. I don't disagree, but didn't want to rewrite the OP' s entire program and leave them trying to work out what had been done. – Andy Brown Jan 31 '15 at 14:26
    
@AndyBrown point taken – RandomHuman Jan 31 '15 at 14:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.