Judy
Judy

Reputation: 1553

How do I check for the null value in Java?

How to look for the value whether it is null or not in JSP. E.g. in below I want to check the description column in pipe table for null values.

String Query1 = "SELECT description FROM pipe where pipe_id='" + id+"' and version_id=2";
SQLResult = SQLStatement.executeQuery(Query1);
SQLResult.first();
description = SQLResult.getString("description");
if(description=="")
{
    json_string=json_string+"&desc="+description;
    out.println("not null");
} else
{
    json_string=json_string;
    out.println("null");
}

Upvotes: 0

Views: 4643

Answers (4)

hkadejo
hkadejo

Reputation: 162

I use this personal method:

public static boolean isNullOrEmpty(Object obj) {
    if (obj == null || obj.toString().length() < 1 || obj.toString().equals(""))
        return true;
    return false;
}

Upvotes: 1

Peter DeWeese
Peter DeWeese

Reputation: 18333

If you are really using jsp, you should store description in a request attribute and then use <c:if test="${empty description}">.

Upvotes: 1

Bozho
Bozho

Reputation: 597106

Or you can use StringUtils.isNotEmpty(description).

Upvotes: 1

kukudas
kukudas

Reputation: 4934

Well: if (description != null && !description.isEmpty()) ... however are you writing this code within a JSP ? That seems wrong. You should probably do this stuff within a servlet and pass the description as an request attribute to a jsp.

Upvotes: 2

Related Questions