This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
posted
0
Hi, If the name is null then else part will get executed.
Please ignore post, I have no idea what I am talking about.
Francis Siu
Ranch Hand
Joined: Jan 04, 2003
Posts: 867
posted
0
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
posted
0
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
posted
0
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.
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
posted
0
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
posted
0
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
posted
0
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
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: 18670
posted
0
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
posted
0
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.
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
posted
0
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: 18670
posted
0
That sounds familiar for some reason.
Majid Khan
Ranch Hand
Joined: Oct 02, 2000
Posts: 92
posted
0
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.