aspose file tools*
The moose likes Beginning Java and the fly likes Null Pointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Null Pointer Exception" Watch "Null Pointer Exception" New topic
Author

Null Pointer Exception

seshayya krishna
Greenhorn

Joined: Feb 24, 2005
Posts: 11
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

Joined: Jul 30, 2003
Posts: 804
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:



Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
seshayya krishna
Greenhorn

Joined: Feb 24, 2005
Posts: 11
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Advanced?

Advanced?

Moving to Java in General (Beginner.)


[Jess in Action][AskingGoodQuestions]
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
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...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Ravi Kishore
Greenhorn

Joined: Mar 22, 2005
Posts: 15
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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

Joined: Jun 08, 2002
Posts: 3178
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

Joined: Mar 22, 2005
Posts: 15
thank u ernest and ko ko nang.

I'll definitely consider u r suggestion in developing my applications.
Anselm Paulinus
Ranch Hand

Joined: Sep 05, 2003
Posts: 389
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

Joined: Oct 02, 2003
Posts: 11311
    
  16

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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

Joined: Jun 08, 2002
Posts: 3178
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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

Joined: Sep 05, 2003
Posts: 389
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null Pointer Exception