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

Checking for null inputs

 
Majid Khan
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
How does one check for null values from input
what happens when I try this :
if (name != null) {}
else {};
what if name is 'null', is the Else ever executed?
please elaborate on this.
I get in the above case null pointer exception.
regards,
Majid
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If the name is null then else part will get executed.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is "name" a string?
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Majid
One method you can use try{} and catch {} box to check it
 
Majid Khan
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,
I guess this does not seem as simple as it looks.
The qstn how can one can compare null input with null.
I want to avoid null ptr exception.
In this case I have converted name to a string ,
Even checking the original object gives null ptr exception.
I hope you can elaborate all cases.

thanks
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what the problem is.
Let's say we have this:

Or you can do this:

Or you can do this:


Why not give us a bit of a better description of the problem you are having.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Majid - if you're getting a NullPointerException, look at the stack trace, and find out exactly which line number is throwing the exception. Study that line. If you can't figure out why it's a problem, post the code here (especially the code of the line that throws the exception) and we'll see what's going on.
 
Mary Leners
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about:

try
{
name = request.getParameter("NAME");
}
catch (nullPointerException npe)
{
// set name equal to "" or whatever you want to
// do when you encounter a null
}
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mary, your solution won't throw a NullPointerException (unless the request object is null but that should never happen). In any case, throwing an exception is EXPENSIVE on system resources and should never be done if it doesn't have to be.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Majid, you really need to explain your problem in more detail. Post any error messages you get (compiler error or run-time exceptions). Also, post the relevent code to give this whole situation some context. In particular, post the method that is causing you problems. Also post declarations of any class-level variables. We will be much better able to help you from there.
Layne
 
Majid Khan
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI ALL ,
THKS for your inputs here is more of code
SessionDictionary sessionDictionary = p_formSession.getSessionDictionary();
name = sessionDictionary.getPropertyValue((String)getAttributeValue(2)).toString()
this is the error i get:
java.lang.NullPointerException
at tblreferenceinsert.evaluate(tblreferenceinsert.java:78)
line 78 is where i get name value
name =.........
any pointers?
this is the error i get :
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NullPointerException always means that if you look at the line that's throwing the exception and you look for dots (.), and then you look at whatever is immediately to the left of a dot, then one of those expressions to the left of a dot is equal to null. So in this line
name = sessionDictionary.getPropertyValue((String)getAttributeValue(2)).toString()
either sessionDictionary is null, or getPropertyValue() returns null. You can rewrite this to add some debugging statemtents:

Something is null. Find out what, and then study why it's null, and think about what to do about it.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on which part of the exception stack trace is being considered, the culprit null reference could also be an argument to a method invoked on that line.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. To clarify, my previous statement assumed that we're looking at the very first line of the stack trace, which is the second line overall (after the message "java.lang.NullPointerException"). E.g.
java.lang.NullPointerException
at tblreferenceinsert.evaluate(tblreferenceinsert.java:78)
at tblreferenceinsert.someOtherMethod(tblreferenceinsert.java:1234)
at tblreferenceinsert.yetAnotherMethod(tblreferenceinsert.java:5678)
Here we only care about the first "at" line, which tells us the exception is being thrown from line 78. In this case the exception is not being thrown from some other method called by line 78; it's being thrown from 78. Which means something on that line is null. And it's something that has a dot immediately to the right - that's what causes the NullPointerException, a null followed by a dot.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
name = sessionDictionary.getPropertyValue((String)getAttributeValue(2)).toString()
sessionDictionary might be null or the getPropertyValue() method is returning null.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds familiar for some reason.
 
Majid Khan
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Thanks for answers .
Also many thanks to Jim, his comments did help to sort the problem.
the session dictionary object throws null.
In this case will an if else statement work or will it return the same answer for both cases ?
I explicitly used multiple if statements.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic