Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Check null value question?

 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I want to check if bug is empty...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Elahe Shafie
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it and thanks every one.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic