getting a nullpointerexception that I can't figure out

so I've been trying to troubleshoot this for a while now. My problem is that I keep getting nullpointerexceptions that stop my code at inopportune times. I get the exception in a try block, but I've narrowed it down to where it occurs, although I can't for the life of me figure out why.

String readline ="";

BufferedReader br =new BufferedReader(new FileReader(filename))

while(!readline.equals(null)){

readline = br.readLine();

System.out.println(readline);

if(readline.contains("..........."))// nullpointer here

}

I believe I get a nullpointerexception when I call readline.contains(), so I print out readline before and it is neither empty nor null.

thanks

[1025 byte] By [snoboardera] at [2007-11-27 8:26:59]
# 1

Try this:

while(readline != null){

readline = br.readLine();

System.out.println(readline);

if (readline.contains("..........."));

}

You want to compare the reference of the variable readline (i.e. whether it's a valid object) and not its content, so you must use the != operator.

readline.equals(null) holds always false so your code is inside

while (true) {

...

}

and is always executed.

I think.

java_knighta at 2007-7-12 20:16:36 > top of Java-index,Java Essentials,New To Java...
# 2
>readline = br.readLine();readline might be null right now, because it was assigned here> if(readline.contains("..........."))//Well, like I said, readline might be null here. You're not testing for null at the right place.
warnerjaa at 2007-7-12 20:16:36 > top of Java-index,Java Essentials,New To Java...
# 3

while(!readline.equals(null)){//line 1

readline = br.readLine(); //line 2

System.out.println(readline); //line 3

if(readline.contains("...........")) //line 4

} //line 5

1: readline is not null so I go into the while loop

2: readline gets a new value

3: readline outputs that value

4: readline uses its contains method

1: readline is not null so I go into the while loop

2: readline gets a null value

3: System.out.println() outputs null

4: readline is told to use its contains value,

unfortunately readline is null: EXPLODE!!!

RedUnderTheBeda at 2007-7-12 20:16:36 > top of Java-index,Java Essentials,New To Java...
# 4

thanks, that's on the right track for sure.

the problem still persists - I know a little more now though

I inserted the following immediately after readline = br.readLine(), and it confirmed that readline == null despite the parameters of the while loop.

if(readline == null)

System.out.println("here"):

I changed the loop to while(readline != null) as well

I'm confused as to why the loop continues when readline is clearly == null

Message was edited by:

snoboarder

snoboardera at 2007-7-12 20:16:36 > top of Java-index,Java Essentials,New To Java...
# 5
try br.readline != null
deAppela at 2007-7-12 20:16:36 > top of Java-index,Java Essentials,New To Java...
# 6
I assume you mean br.readLine()?I can't start doing that because then the bufferedreader starts skipping over lines, that always causes a mess
snoboardera at 2007-7-12 20:16:36 > top of Java-index,Java Essentials,New To Java...
# 7

> I'm confused as to why the loop continues when

> readline is clearly == null

Because you're changing readline INSIDE the loop, AFTER it has already been tested for null.

while (something)

doesn't mean "as soon as (something) is false, get out of the loop even if you're in the middle of it".

It only evaluates (something) at the TOP of the loop, ONCE per iteration. If you change (something) inside the loop, it doesn't automagically re-evaluate the expression and get out immediately.

warnerjaa at 2007-7-12 20:16:36 > top of Java-index,Java Essentials,New To Java...
# 8

Read the line and check for null at the same point

while((readline = br.readLine()) != null)

{

if(readline.contains(...))

// do something

}

SomeoneElsea at 2007-7-12 20:16:36 > top of Java-index,Java Essentials,New To Java...
# 9
> Read the line and check for null at the same pointHow embarrassingly stupid. How did I not see that ?! And I've used this code plenty of times.... Time to go home from work ;-)
java_knighta at 2007-7-12 20:16:36 > top of Java-index,Java Essentials,New To Java...
# 10
We've all done stuff just like it. Go home, drink a glass of your favorite adult beverage, and get some sleep.~Tim
SomeoneElsea at 2007-7-12 20:16:36 > top of Java-index,Java Essentials,New To Java...
# 11
hey, don't feel too stupid, I had no idea you could even do that
snoboardera at 2007-7-12 20:16:36 > top of Java-index,Java Essentials,New To Java...
# 12
> hey, don't feel too stupid, I had no idea you could even do thatEmbedding assignments in looping conditions goes way back -- it's very commonly done in C.When I'm coding in VB I miss it :-(
Hippolytea at 2007-7-12 20:16:36 > top of Java-index,Java Essentials,New To Java...
# 13
> I assume you mean br.readLine()?> I can't start doing that because then the> bufferedreader starts skipping over lines, that> always causes a messyes, typo its br.readLine() != null
deAppela at 2007-7-12 20:16:36 > top of Java-index,Java Essentials,New To Java...