charAt() causes null pointer exception help
in
Programming Questions
•
8 months ago
I am loading a file and checking to see if if the line starts with a #. If it does(just to see that it works) print COMMENT
However on line 10, it throws a Null Pointer Exception. When i test it without using the charAt method of string, it does not have any errors so it would be: if(line == "#"), but that is not what i want.
Here is my code:
BufferedReader reader;
String line = "";
void loadDB()
{
while(line != null)
{
line = loadLine();
println(line);
if(line.charAt(0) == '#')
{
println("COMMENT");
}
}
}
String loadLine()
{
try
{
return reader.readLine();
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
I have no idea why it does this any help?
1