• Post Reply Bookmark Topic Watch Topic
  • New Topic

Strange NullPointerException phenomenon  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am facing this strange NULL POINTER EXCEPTION phenomenon.

In my programme, i have a String Variable which is populated by a method from a different java class in the same package. In order to counter a Null Pointer exception (incase the method that is being called for returns a null), i am using try catch block. Now my query is, the try catch block works perfectly if i use a IF condition as is shown below, else the try catch block is never reached. The code is :

try{

String abc = Diffclass.getString();
if(abc.equalsIgnorecase("null"))
{
SOP(" something "); //this however does not get printed even if the try catch works
}
}
catch(NullPointerException ex)
{
SOP;
Continue;
}

if i remove/comment the IF condition the try-catch block is not executed.

am i missing something important here and so i find this strange ? or what is actually happening.
 
Kannan Venkateshan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

This indeed was a very simple question and i am surprised that i was unable to figure it out myself.

This is what happens i guess :

1) in the first instance when i am removing/commenting the "if" condition, the variable in which i am storing the value is not being checked or used explicitely - so we have the system checking in for the exception and throws one exiting the program.

2) when we give an explicit "if" condition, the variable is used to check a condition and becasue the contents are "null" - an explicit exception is thrown. And this is caught in the try-catch block.

Thanks and regards,

Kannan
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you don't get a null pointer exception is because the getString() method does not throw one. Instead it simply returns null. The reason the "if" triggers a null pointer exception to be thrown is this line.



When this line tries to dereference the abc variable (in order to call the equalsIgnorecase() method), it will cause a null pointer exception to be thrown.

BTW, you don't need the try-catch at all. You could have done this...



Henry
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kannan,
the try catch block works perfectly if i use a IF condition

You are getting Nullpointer Exception here, because you are trying to invoke the method (equalsIgnorecase()) on a null object.Whenever you try to manipulate a object which is null then Null pointer exception is thrown
if(abc.equalsIgnorecase("null"))

If you want to compare whether the String object returned by Diffclass.getString() method is null or not then you must code it like this:
if(abc==null)

else the try catch block is never reached

When you comment if condition in your code, then you are not manipulating the returned String object, so an exception is not thrown when that returned string object is null.

Regards,
Sujith
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!