aspose file tools*
The moose likes Java in General and the fly likes How to compare String with null ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to compare String with null ??" Watch "How to compare String with null ??" New topic
Author

How to compare String with null ??

R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Very simple question, but just want to share with you all.
Till now I was comparing String with null like this:
if(myString == null) {
// do something
}
Currently I am using Tomcat4.1, I will check which JVM it is using and tell you and Ecliipse IDE.
First time I have to compare null like this:
if(myString.equals("null")){
// do something
}
I remember long back in 2000/1 once we faced this problem, two teams were working. One team had JWS server and other had JRun.
I cant recollect what was the exact problem. But we had lot of problem in running our code on JRun.
That was just because JRun was checking null in different way than JWS.
How to make code portable on all Server/JVM ?
Is Java becoming like C ??
Write server dependent code.


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Trelan Hylton
Greenhorn

Joined: Apr 15, 2003
Posts: 27
myString==null is different from myString.equals("null");
in the second case myString may have a string "null". the first case is that the variable myString may not yet been instantiated. to check if myString is an object but empty use myString.equals("");
[ May 01, 2003: Message edited by: Trelan Hylton ]

JH
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
mystring.equals("null") is asking if mystring is a string that contains the word "null". It is no different than asking if mystring.equals("dog").
Perhaps somewhere before you get hold of the string someone is initializing the string with the word "null" if the string == null?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Another (more likely?) possibility is that the String was written out to an output stream or file somewhere without checking to see if it was null first.

prints "null" instead of ""; If this is written to a file and subsequently read back in, suddenly the String is "null" instead of null (or "", depending on how things get read in).


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Good point, Joel. String concatenation with "+" has the same effect, and is pretty common. E.g.
"Value: " + val -> "Value: null"
Though this usually won't give you a sting with "null" and nothing else, unless you later parse the string to isolate its parts.


"I'm not back." - Bill Harding, Twister
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Joel McNary:
Another (more likely?) possibility is that the String was written out to an output stream or file somewhere without checking to see if it was null first.

prints "null" instead of ""; If this is written to a file and subsequently read back in, suddenly the String is "null" instead of null (or "", depending on how things get read in).

Looks like this is the case as I am using Tomcat inside the eclipse. So when I start the Tomcat, it runs inside the Eclipse.
Here is the output from the Eclipse console:
==========
inbox.jsp : strMId : null
inbox.jsp : (strMId == null) : false
inbox.jsp : (strMId.equals(null)) : false
inbox.jsp : (strMId.equals("null")) : true
==========
But then in this case, when I am converting strMId in to an integer, how am I getting NullPointerException , as null has been converted to string ??
Thanks for your input.
Do I have to change the code while running on independent tomcat ? Currently tomcat is running inside eclipse[using tomcat plugin].
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Good point, Joel.
Don't be so hasty Jim. There was not one zoological metaphor in his post. I think he's losin' it.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
But then in this case, when I am converting strMId in to an integer, how am I getting NullPointerException , as null has been converted to string ??
Good question; this makes no sense. What does your code look like to do this conversion? And are you certain that the value of strMId is the same one as when you get results such as
inbox.jsp : (strMId.equals("null")) : true
?
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Jim Yingst:
Good question; this makes no sense. What does your code look like to do this conversion? And are you certain that the value of strMId is the same one as when you get results such as
inbox.jsp : (strMId.equals("null")) : true
?

It is like this:
if( ! strMId.equals("null") && ! strMId.equals("") && messId == Integer.parseInt(strMId) )
When I got first time NPE. I thought, there is problem in JVM and it evaluating if statement from right side.
I wrote code like this:
if( ! strMId == null)) {
if(! strMId.equals("") && messId == Integer.parseInt(strMId) ) ){
// do something
}
}
But still I was getting NPE error.
So I print all possible comparison.
And then I found that it return true only when I compare with string "null".
Now when I am passing MId null, there is no error.
But if I use strMId == null, I am getting NPE.
My only problem is that ... do I have to change the code for final deployment.
I think, I will check it on seperate tomcat installation/ machine tomorrow.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Sounds like you're trying to put too much in one line, without being sure what's going on. This is particularly difficult if you're getting a NullPointerException - you can't tell which part of the line it's coming from. Break out the different cases into separate lines and write clear messages to see what's really going on, e.g.

Here "out" is a PrintStream/PrintWriter to wherever you want to put your output.
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Jim Yingst:
Break out the different cases into separate lines and write clear messages to see what's really going on, e.g.

Hi Jim
Thanks for your time.
here is the code

and here is the output:
===============
inbox.jsp : strMId : null
inbox.jsp : (strMId == null) : false
inbox.jsp : (strMId.equals(null)) : false
inbox.jsp : (strMId.equals("null")) : true
inbox.jsp : The string "null" is not a number. Exception is java.lang.NumberFormatException: null
===============
sop() is private method:
<%!
private void sop(String str) {
System.out.println("inbox.jsp : " + str);
}
%>
How come once null is "null" and then it again becomes null.
Murali Nanchala
Ranch Hand

Joined: Mar 14, 2001
Posts: 74
Ravish,
Would you just copy and paste the line where you declare the
String strMId
in the JSP.
Just to make sure we don't have a problem there.


"It is almost always answered in the API" - Murali Nanchala
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
It is coming as parameter, which I am not sending.
AW now I will see this problem tomorrow .. saturated .. going home.
Murali Nanchala
Ranch Hand

Joined: Mar 14, 2001
Posts: 74
Exactly, whatever is sending the parameter in, is sending a String "null" instead of a null String Object.
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Murali Nanchala:
Exactly, whatever is sending the parameter in, is sending a String "null" instead of a null String Object.

I mean.. I have not created any input param in Html for it till now. So getParameter() will return null only.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
How come once null is "null" and then it again becomes null.
In the code you've shown here, it's the String "null", period - it is never null. You said earlier that you get a NullPointerException, but I don't see any code that throws NullPointerException in what you just showed us. Do you really still get NullPointerException somewhere? Find the code that throws it, and insert the tests from my last post, immediately before the code that throws NullPointerException. Then you will know exactly what strMId is, immediately before it's used.
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
I think because small font size of CODE block you missed it.
Code is:
try {
int val = Integer.parseInt(strMId);
sop("It's a number: " + val);
}catch (Exception e){
sop("The string \"" + strMId + "\" is not a number. Exception is " + e);
}
And output is :
inbox.jsp : The string "null" is not a number. Exception is java.lang.NumberFormatException: null
This code and output is available in second last post.
sop() is private method which simply prints on console.
Thanks in advance
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

I'm confused--Are you getting NumberFormatExceptions or NullPointerExceptions?
And where is strMId coming from? Is it a formal parameter to a method, or is this in a servlet/JSP and strMId is a parameter obtained from a call to request.getParameter()?
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
It is NumberFormatExc here ..
but if you see the printstack ... it says the NullPoniter while calling method of Integer.
Now I cant go back and give the printstack...
please wait .. I have bypassed this for the time being .... will be back by next friday or sunday ... with the exact problem or with the solution.
Thanks for your time and effort ..
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If you want to ask about why you get a NullPointerException, please show code that actually throws a NullPointerException, and show the stack trace you get. Using the code shown so far the problem is clearly a NumberFormatException caused by the fact that the string "null" is not a number. I don't get any NullPointerException in my stack traces here.
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Jim Yingst:
If you want to ask about why you get a NullPointerException, please show code that actually throws a NullPointerException, and show the stack trace you get. Using the code shown so far the problem is clearly a NumberFormatException caused by the fact that the string "null" is not a number. I don't get any NullPointerException in my stack traces here.

Hi Jim & all
sorry ... its a null string I am getting ..
Looks like I was tired last night thats why NumberFormatException was looking NullPointerException to me.
Actually right now I am converting one code to compatible to Struts.
I just checked it.. actually its assigning "null" though I am not passing mId.
But the same code on the current server does not assign "null".
I have just mailed to ask currently it is running on which server.
I will inform you all...
Sorry to all... I think one should take proper sleep before working.
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
AW guys I am back ... [extreamly sorry for my foolish problem of NullPointerException ]
Here is the printStack which confused me:
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:394)
at java.lang.Integer.parseInt(Integer.java:476)
.
.
.

But Now question comes, how to make code SERVER independent ?
The following code is in file01.jsp
src="Inbox.jsp?contractId=<%=inboxForm.getFleetId() %>&mId=<%=request.getParameter("mId")%>
In Inbox.jsp there is this code which works perfectly on Weblogic.
if( strMId != null && ! strMId.equals("") && messId == Integer.parseInt(strMId) )
But the same code fails in Tomcat. The reason is that in weblogic, in file01.jsp, if "mId" param is not available then req.getParameter() does not return anything.
But in Tomcat returns null, which in next page [in inbox.jsp] becaomes String "null".
How to make sure that code will run on all server?? :roll:
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, the simplest fix based on what you have is to add a check for "null":

Or you can just do something like this:

Whatever value messId has, it can be converted into a String representation - which is definitely not null, or "null" - and can be safely compared to strMId.
Personally, any time I get a String input from a potentially dubious source, I like to do a trim() as well to make sure there aren't any icky spaces which will mess up equals(). So:

[ May 04, 2003: Message edited by: Jim Yingst ]
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Now I have done the same as you are suggesting
Thanks Jim.
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
why do you want to compare string and null? you can use 'if' statement to make sure two strings to be compared are not null first.
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Only for Don Liu:
Originally posted by Ravish Kumar:

Till now I was comparing String with null like this:
if(myString == null) {
// do something
}

And still I do so ...
OR are you really android ? who is deleting same message and posting it again ??
 
 
subject: How to compare String with null ??