Link to home
Create AccountLog in
Avatar of poweranger77
poweranger77

asked on

check for null

hi, i am having this code
<%
String main_message = (String)request.getAttribute("main_message");
String sub_message = (String)request.getAttribute("sub_message");
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.NullPointerException pointing to if (!main_message.equals(null) || !main_message.equals("")) {

but if i put like this :

<%
String main_message = (String)request.getAttribute("main_message");
String sub_message = (String)request.getAttribute("sub_message");
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
Avatar of avinthm
avinthm

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer