• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

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.
0
poweranger77
Asked:
poweranger77
1 Solution
 
avinthmCommented:
> if (!main_message.equals(null) || !main_message.equals(""))

if (main_message != null && !main_message.equals(""))
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now