Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String null check

Tags:

java

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>
like image 742
Pawan Avatar asked Sep 12 '26 08:09

Pawan


1 Answers

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

 if (value == null || value.equals("")) {
     value = "Anonymous";
 }
like image 150
ig0774 Avatar answered Sep 14 '26 06:09

ig0774



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!