aspose file tools*
The moose likes Beginning Java and the fly likes Check null value question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Check null value question?" Watch "Check null value question?" New topic
Author

Check null value question?

Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Hi Friends,
I want to check if bug is null (bug is object) but I catch the error exception:
Exception occured
Message: $exceptionmsg
String: java.lang.NullPointerException
Could you please correct me what part I am wrong in syntax?
Many thanks,
ELahe


Thanks,
Elahe
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Sorry I want to check if bug is empty...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Your code:

is not a check for null. What you are comparing bug against in this case is an empty String instance.
To check an object reference for null you'd use:

hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

OK, so you say you want to check that bug is "empty". Well, what is bug? How is its equals() method defined?
In your code, you are checking bug against an emtpy String instance. Is that the check you want to make?
I guess we'd need a bit more information on what you expect "check if bug is empty" to mean in the context of what exactly bug is.
bear
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Assuming you do indeed need to check to see if bug is an empty string (""), the problem seems to be that you first need to check for null, before you can use .equals() or any other instance method. There are a lot of ways to achieve this:
orororor
But if you find it necessary to do this sort of thing more than once in your code, I'd favor making a utility method to handle the sitation, since a named method will be simpler to use elsewhere:

[ June 10, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jim Yingst:

That wouldn't work - you need to use the "shortcut-or" operator, so that the second operand doesn't get evaluated when the first is true:


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Dear friends,
To answer your question, bug is instance an object that calls Bug and I try to check if bug is empty then pass null to my template.
So I tried this way as you suggested but do I need to have this line as well?
bug = null;
thanks,
Elahe
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
That wouldn't work - you need to use the "shortcut-or" operator
Thanks - typo. That's what I get for listing so many different versions. :roll:
So I tried this way as you suggested but do I need to have this line as well?
bug = null;

Not unless you do something else with bug afterwards. I was assuming it was a local variable that you would forget about afterwards, but if that's not the case, you'll need to decide for yourself whether you want the value to be "" or null later on.
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Got it and thanks every one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Check null value question?