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
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: 18670
posted
0
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
posted
0
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
posted
0
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: 18670
posted
0
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.