0

If the newWord is null ,it should not go in the loop,but why does it go inside the loop and gives java.lang.NullPointerException

newWord = "abcd";
while(!newWord.equals(null))
{
    try {
    newWord = br.readLine();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    }
    catch(NullPointerException p)
    {

    }
}

It gives the stacktrace but i have not used printStackTrace() anywhere

8 Answers 8

8

newWord itself is null. When an object is null, you can't call any methods on it as the object is not defined. As .equals is a method, you are getting an exception. Try this instead:

newWord != null

This is a problem easily solved by debugger. Learning to use a debugger is frustrating (as is learning any new tool,) but it will save you many hours of pain. It is your friend.

Sign up to request clarification or add additional context in comments.

1 Comment

@JohnSnow I'm glad! Remember to upvote anyone who helped. It helps our fragile egos :-) Also accept the answer you found most helpful.
3

how about simply

    while(newWord!=null)

Think about it, If newWorld is null, what happens when you call methods on it ?

1 Comment

Don't know, +1 to fight the haters.
1

It doesn't go into the loop because

newWOrd.equals(null)

will throw an NPE if it is null

What you meant was

newWord != null

You can see this behaviour if you use a debugger, or look at the line in your stack trace where it is triggered.

Comments

0

You only check if "newWord" is null, never if "br" has anything left reading from.

Something like:

while(br.hasNext());

Comments

0

the NullPointerException you are getting is because of while(!newWord.equals(null)) and it is not caught because try , catch were used after this code.

If you want to suppress this exception to then put that while(!newWord.equals(null)) in try-catch block.

1 Comment

Suppressing the exception is not the solution here.
0
!newWord.equals(null)

is your problem.

Use newWord != null

2 Comments

String's equals method is never entered as anObject in this case is null. The line if(anObject instanceof String) is fine by itself if anObject is null.
Ah, much better. Good job, +1
0

The argument for String.equal should not be null and explicitly null is passed so is the exception. Use compound expression to check for empty string and special null.

It tried to evaluate the expression before entering loop, soon the expression is encountered !newWord.equals(null), because of newWord being null an exception were thrown.

1 Comment

Passing null into String.equals is fine. The problem is newWord is null.
0

to avoid null exception in most of the cases (inside conditional expressions) I advise you to use Yoda conditions:

if(null != value)

Furthemore, this apply to every condition between a variable and a constant:

if(MY_CONSTANT_STRING.equals(myVariableString))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.