Reputation: 257
I'm getting a NullPointerException
on a line where I call a method and pass it a string. I can only assume the string is null.
It was initialised from a BufferedReader
readline a few lines before. Here's the relevant code:
FileInputStream tmp = null;
try {
tmp = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.err.println("File not found!");
e.printStackTrace();
System.exit(1);
}
DataInputStream dis = new DataInputStream(tmp);
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
create();
try {
data = br.readLine();
} catch (IOException e) {
System.err.println("First readline failed: " + e);
System.exit(1);
}
while (!data.equals(null)) {
process(data);
...
and the error:
Exception in thread "main" java.lang.NullPointerException
at enc.read(enc.java:40)
at enc.main(enc.java:15)
Upvotes: 1
Views: 1667
Reputation: 10151
while (!data.equals(null))
makes no sense. If data
is null
you cannot call equals(..)
on it.
Replace
while (!data.equals(null))
by
while (data!=null)
Upvotes: 1
Reputation: 38521
If I had to guess, I think the bug is in:
while (!data.equals(null)) {
process(data);
try changing it to:
while (data != null) {
process(data);
}
You can't invoke a method from a null object.
Upvotes: 2
Reputation: 35008
Without knowing the line numbers, I would suspect that the following is the culprit:
!data.equals(null)
If data
is null
, then a NullPointerException
will be thrown.
Replace it with:
data != null
Upvotes: 6
Reputation: 5843
Your nullness check itself triggers the NullPointerException
. Instead of !data.equals(null)
write data != null
.
Upvotes: 8