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.
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 ]
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?
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).
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.
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].
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 ?
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.
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.
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.
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.
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
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()?
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 ..
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.
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.
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:
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: