Here are snippets from my code:
class Lines{
int nameMax() throws IOException{
// initialize variables
// These will be the largest number of characters in name
int namelen = 0;
//These will be the current word lenght.
int namelenx = 0;
...
while(name != null){
name = br.readLine();
namelenx = name.length();
...
if(namelenx > namelen) namelen = namelenx;
}
float nameleny = namelen/2; //Divides by 2 to find midpoint
namelen = Math.round(nameleny); //Value is rounded
return namelen;
}
}
I am using BlueJ, and whenever I try to run this it is giving me the error in the title and highlighting namelenx = name.length(); There are String variables for name as it was part of the code I cut out. Helping answers please. Thanks.
It throws NPE when
br.readLine()returnsnull, as you invokelength()on null.your while loop should be something like below:
Now, even if
bufferedReaderreturns null onreadLine()your while would terminate.