We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

check for null

poweranger77
poweranger77 asked
on
Medium Priority
217 Views
Last Modified: 2010-04-01
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.
Comment
Watch Question

Commented:
> if (!main_message.equals(null) || !main_message.equals(""))

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.