• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null Pointer Exception

 
seshayya krishna
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am checking for null values as below. Normally if we check for null
values we can escape NULLPOINTEREXCEPTION. But in this case still I am
getting NULLPOINTEREXCEPTION.

I want to continue next statement even if the following is null.
What should I do ? Please help me ASAP.


if (grantApplicationDoc.getGrantApplication().getForms().getKeyContactPersons() != null)
{
keyContactPersonFirstName = grantApplicationDoc.getGrantApplication().getForms().getKeyContactPersons() ;

}


Murali

[EJFH: Removed "Very urgent" from subject line. ]
[ March 30, 2005: Message edited by: Ernest Friedman-Hill ]
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, what's the "very urgent" situation? I see no urgency here.

Second, the exception is probably thrown in one of the methods in your chain:

 
seshayya krishna
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I walked thru debugger, the exception is occuring at that point.
Can you help me ? Not in other places.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Break up the line with intermediate variables:

This will help you step through the debugger to find which reference is null. You might want to add display messages or throw your own exceptions in a real life application.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Advanced?

Advanced?

Moving to Java in General (Beginner.)
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seshayya,
If u want to continue your application, even when there is a null value, you can do like the following...


By using the above approach, you just create a new object instead of assigning a null value to the keyContactPersonFirstName variable, which can let you continue the execution... Hope this helps...
 
Ravi Kishore
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi seshayya krishna,

if u want to continue even when a exception occurs u can do the following:

1) u can follow the else condition solution given by ko ko nang.

2) use try-catch block as shown below:

try{

keyContactPersonFirstName = grantApplicationDoc.getGrantApplication().getForms().getKeyContactPersons() ;

}catch(NullPointerException e)
{
keyContactPersonFirstName = "some string";
}

before applying the solution chect what does the following statement return "grantApplicationDoc.getGrantApplication().getForms"().getKeyContactPersons()
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Stan. One of the methods is probably returning a null which is then being used to call another method in your chain. Look at Stan's suggestion to help you figure out which method is returning null. I suspect this means there is a bug somewhere since you are not expecting this null to be returned.

Layne
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ko Ko and Ravi --

You're not helping this guy by teaching him bad coding habits. In particular, catching a NullPointerException the way Ravi has shown is a horrible, horrible, horrible thing to do. Check for null, and act on it if necessary. Never allow an NPE to happen, and clean up afterwards. It's sloppy, inefficient, and ugly.

Instead, everyone take a close look at what Stan wrote above. He hit the nail on the head. Don't chain a bunch of method calls together unless you know for certain that none of the calls will ever return null -- and it's quite clear that seshayya krishna has no such assurance.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi, we might need to grab a Java Best Practices book so that we can provide others with the codes in the best quality in the world... Checking null chain as Stan suggested is the best way to adopt here in this case...

So , you can now assure that Stan's way of coding is the best out here, make use of it and adapt it in your applicaiton....
 
Ravi Kishore
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u ernest and ko ko nang.

I'll definitely consider u r suggestion in developing my applications.
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ko Ko:
You were more guilty than Ravi, because you were the one who initiated the bad practices, just to turn around to advice him on good practices. I guess you need to get that book on good practices today and stop teaching us bad practices!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EFH wrote:
...is a horrible, horrible, horrible thing to do.

Advanced?

Advanced?

don't hold back, Ernest. Tell us how you REALLY feel about this.



One of the old Guru's here used to say "one line of code should do one thing". the line of code that does this:

grantApplicationDoc.getGrantApplication().getForms().getKeyContactPersons()

does three things (at least). sure the debugger told you the problem was on this line, but it doesn't (and pretty much CAN'T) tell you WHERE on this line it happened.

Once you break up the code like Stan said, the debugger will show you which one is returning the null
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that chain of method calls is not a lovely thing. Demeter is frowning on that one. I only went as far as a debugging suggestion.

A (rather advanced) approach is to guarantee that none of your getWhatever() methods ever return null. The NullObject pattern (practice?) returns a special polymorphic substitute object that implements the proper behavior for a missing object. Getting your head around the proper behavior for a missing object is why it's kinda advanced.

For example, we use a framework that lets you build objects with automated transaction management or manual transaction management. They had this in the training:

What if getTransaction returned a special Transaction that did nothing at all?

No tests for null. No null pointer exceptions.
[ March 31, 2005: Message edited by: Stan James ]
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anselm Paulinus:
Ko Ko:
You were more guilty than Ravi, because you were the one who initiated the bad practices


Well, you might be too good in best practices becasue you can judge on someone else who is more guilty than who... If I accidently provided someone some codes with bad practices, that's my own fault that I am weak in those areas... But I've never seen someone who is judging like he/she is guilty or he/she is innocent... That's radiculous... No one can be perfect...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fred rosenberger:

don't hold back, Ernest. Tell us how you REALLY feel about this.


Fred's been following me around

Sorry, I've been kind of cranky lately. Not enough sleep, too much toddler care.
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ko Ko Naing:


Well, you might be too good in best practices becasue you can judge on someone else who is more guilty than who... If I accidently provided someone some codes with bad practices, that's my own fault that I am weak in those areas... But I've never seen someone who is judging like he/she is guilty or he/she is innocent... That's radiculous... No one can be perfect...


Koko; you sure did not take that serious; you sure knew I was kinda joking.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic