• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Strange NullPointerException phenomenon

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23926
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic