2

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>

<%!
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>
3
  • Try this: if (value==null || value.trim().length()==0 || value.equalsIgnoreCase("null")) {value = "Anonymuos";} Commented Sep 23, 2011 at 12:00
  • 1
    Your order of conditions is wrong, First check ==null. In this case if value == null you gets NullPointerException because of value.equals("") is executed first. if (value == null || value.length() == 0) value = "Anonymous"; should be OK. Likely you set value by string "null" somewhere somehow. For example value = String.format("%s", somethingThatIsNull) will result in value == "null". Commented Sep 23, 2011 at 12:00
  • possible duplicate of One step check for null value & emptiness of a string Commented Sep 23, 2011 at 12:03

10 Answers 10

5

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

 if (value == null || value.equals("")) {
     value = "Anonymous";
 }
Sign up to request clarification or add additional context in comments.

1 Comment

@ante.sabo: True, but it's probably actually better to use something like Apache Commons StringUtils#isBlank() to cover that, as others suggested...
3

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.

Comments

3

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.

1 Comment

its still a good practice to always use a literal as the first operand in equals comparison
2

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.

3 Comments

While the fix is correct, the diagnosis is not. "null" is not a null string. It's not returning a null string - it's returning the 4-character string "null".
all is well in this world and this is the accepted answer but i would just like to add here that while string checks its should always be "null".equals(value). i.e. the literal should always be the first operand.. that way you avoid NullPointerException
by null string, i also meant "null" string only and I think that is too obvious based on the check value.equals("null") I have talked about.
1
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))

Comments

1

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"?

Comments

0

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

Comments

0

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

2 Comments

Using a third-party library for something as simple as checking for null seems a little superfluous to me.
Checking for null and empty string. I think the code is more readable with StringUtils and there are other useful method in this lib. With maven just a few clicks to import a dependency, it's a not a big deal.
0

Use this :

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.