开发者

String null check

开发者 https://www.devze.com 2023-04-08 11:15 出处:网络
I have tried this for 2 hours , but I\'ve given up. I am checking the value of a String this way: if(value.equals(\"\")||value.length()<0||value==null)

I have tried this for 2 hours , but I've given up. I am checking the value of a String this way:

if(value.equals("")||value.length()<0||value==null)
{
    value = "Anynomous"
}

But still the string is printing the value null.

Please see this program code here

<HTML>
<BODY&g开发者_如何学运维t;

<%!
String value ;
%>

<%

value = (String)request.getAttribute("retunval");
System.out.println("Inside New fgdf JSP");
if(value.equals("")||value.length()<0||value==null)
value = "Anonymuos";


%>
Hello <%=value%>

<BR>
</BR>

<BR>
</BR>

<a href="Test.jsp">Try Again</a>
</BODY>
</HTML>


Check whether value is null first. Otherwise value.equals() will throw a NullPointerException.

 if (value == null || value.equals("")) {
     value = "Anonymous";
 }


First you need to reorder your if condition, otherwise you could run into NPEs. Additionally, the string length can never be lower than 0, so that check is not needed:

value==null||value.equals("")

Alternatively, if you can use Apache Commons Lang you could use StringUtils.isEmpty(value) which handles all the relevant cases for you.


You need to add

||value.equals("null")

Somewhere something already did a toString() on the null value. Note the other answers as well - but this is the cause of the specific problem you're reporting.

So you'd end up with, as one option:

if ( value==null || value.isEmpty() || "null".equals(value) )

Because of the null-check, inverting the .equals() isn't necessary in this case - but it's a good habit to get into.


i think

value = (String)request.getAttribute("retunval");

is returning you a "null" string. When you try to do if tests that you have mentioned, it will return false because you are not checking

value.equals("null")

so that's why you get output as null.


if(value.equals("")||value.length()<0||value==null)
  value = "Anynomous"

The second and third condition can never evaluate to true! (A String's length can never be less than zero, and if value was null, value.equals("") would have caused a NullPointerException)

try this instead:

if(value==null||"".equals(value))


value.length()<0 will never be true - use value.length()==0 instead. However, in this case, the previous check (value.equals("")) is equivalent to this one, so you can omit it. Apart from this, the condition should detect empty/null strings (except that the null check should be first, to avoid dereferencing a null pointer).

Is it possible that value contains the text "null"?


Is it possible that the value of the string is 'null'? (That is, not null, but a string which reads 'null')


Reorder as Thomas and ig0774 suggested or use the StringUtils.isEmpty() method of Apache Commons Lang.


Use this :

   if(value == null || value.trim().length() <=0 || value.trim().equals("null")){
     value ="Ananymous";
   }


I think this would be the best condition to check for it

if(value==null || value.trim().length()==0 || value.equals("null"))

it also eliminate fact if value=" " contains spaces.

0

精彩评论

暂无评论...
验证码 换一张
取 消