This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Checking for null inputs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Checking for null inputs" Watch "Checking for null inputs" New topic
Author

Checking for null inputs

Majid Khan
Ranch Hand

Joined: Oct 02, 2000
Posts: 92
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

Joined: Nov 28, 2001
Posts: 233
Hi,
If the name is null then else part will get executed.


Sharing is learning
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Is "name" a string?


Please ignore post, I have no idea what I am talking about.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Majid
One method you can use try{} and catch {} box to check it


Francis Siu
SCJP, MCDBA
Majid Khan
Ranch Hand

Joined: Oct 02, 2000
Posts: 92
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

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Mary Leners
Greenhorn

Joined: Apr 03, 2003
Posts: 2
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

Joined: May 05, 2000
Posts: 13974
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

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


Java API Documentation
The Java Tutorial
Majid Khan
Ranch Hand

Joined: Oct 02, 2000
Posts: 92
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: May 05, 2000
Posts: 13974
name = sessionDictionary.getPropertyValue((String)getAttributeValue(2)).toString()
sessionDictionary might be null or the getPropertyValue() method is returning null.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
That sounds familiar for some reason.
Majid Khan
Ranch Hand

Joined: Oct 02, 2000
Posts: 92
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Checking for null inputs
 
Similar Threads
while loop
Code to store null keys and null values in hashmap
Prevent 'null' in sessions..
B&S question about new values in updateRecord
String validation