poweranger77
asked on
check for null
hi, i am having this code
<%
String main_message = (String)request.getAttribu te("main_m essage");
String sub_message = (String)request.getAttribu te("sub_me ssage");
if (!main_message.equals(null ) || !main_message.equals("")) {
out.println("Main Message :" + main_message + ".");
out.println("Sub Message :" + sub_message + ".");
}
%>
if i put as shown above, it'll prompt me java.lang.NullPointerExcep tion pointing to if (!main_message.equals(null ) || !main_message.equals("")) {
but if i put like this :
<%
String main_message = (String)request.getAttribu te("main_m essage");
String sub_message = (String)request.getAttribu te("sub_me ssage");
if (main_message!=null || main_message!="") {
out.println("Main Message :" + main_message + ".");
out.println("Sub Message :" + sub_message + ".");
}
%>
the out.println("Main Message :" + main_message + "."); out.println("Sub Message :" + sub_message + "."); still come out and display in the page.
what is the correct way to check and doesnt want to display if the main_message contain nothing or carry a "" (empty) value.
* the way i set the main_message is using "" to set it.
<%
String main_message = (String)request.getAttribu
String sub_message = (String)request.getAttribu
if (!main_message.equals(null
out.println("Main Message :" + main_message + ".");
out.println("Sub Message :" + sub_message + ".");
}
%>
if i put as shown above, it'll prompt me java.lang.NullPointerExcep
but if i put like this :
<%
String main_message = (String)request.getAttribu
String sub_message = (String)request.getAttribu
if (main_message!=null || main_message!="") {
out.println("Main Message :" + main_message + ".");
out.println("Sub Message :" + sub_message + ".");
}
%>
the out.println("Main Message :" + main_message + "."); out.println("Sub Message :" + sub_message + "."); still come out and display in the page.
what is the correct way to check and doesnt want to display if the main_message contain nothing or carry a "" (empty) value.
* the way i set the main_message is using "" to set it.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.