3

This works:

String address1 = HtmlUtil.escape(rs.getString("address1"));

...but i don't want to display "null". So i tried this, which does not work:

String address1 = HtmlUtil.escape(rs.getString("address1") || "");

Is the best option to use this:?

String address1 = HtmlUtil.escape(rs.getString("address1"));
if (address1 == null) address1 = "";

7 Answers 7

6

Using ternary operator, you can assign a value with single line of code:

String address1 = (rs.getString("address1") != null) ?
                  HtmlUtil.escape(rs.getString("address1")) : "";
Sign up to request clarification or add additional context in comments.

3 Comments

It's bad practice to make identical calls to a method (here, rs.getString); use a temporary variable instead.
@Vulcan I just wanted to give an idea to the OP. Of course he/she can use temporary variable for it. but this has nothing to do with the accuracy of my answer and you shouldn't have downvoted.
I like the ternary operator but in this case my solution is shorter, more readable, and more efficient.
4
String address1 = HtmlUtil.escape(rs.getString("address1"));
if (address1 == null || address1.equals("null")) {
    address1 = "";
}

4 Comments

Because the database also contained bad strings, i'll accept this answer.
If you have multiple variables you have to get from your ResultSet and you need to check for null (or "null"), I would suggest to write a method you can reuse for other variables and perhaps wrap this in a ResultSetWrapper so you keep your code clean.
I would also suggest your get rid of the "null" strings in your database / application. It is an unnecessary overhead.
That was actually a mistake (i didn't realize the escape function would not escape the null), but the field can contain whitespace and i always forget about having to use equals() to reliably compare strings in Java.
1
String address1 = HtmlUtil.escape(rs.getString("address1")) == null ? "" : HtmlUtil.escape(rs.getString("address1")); 

If you want ugly one-liner:-)

Comments

1

Solution:

 String address1 = HtmlUtil.escape(rs.getString("address1"));
 if (address1 == null) address1 = "";

1 Comment

I suppose this is the most efficient solution.
1

Use a temp variable to only call HtmlUtil.escape once. Then check for null and assign to address1:

String tempAddress =  HtmlUtil.escape(rs.getString("address1"));
String address1 = tempAddress != null ? tempAddress : "";

1 Comment

I'd rather not create additional variables.
0

Try something like this

<c:when test="${empty rs.getString("address1")}">
  ...
</c:when>
<c:otherwise>
   .....
</c:otherwise>

Comments

0

you can try this :

String address1 = "";
if(HtmlUtil.escape(rs.getString("address1") != null && !(HtmlUtil.escape(rs.getString("address1").equalsIgnoreCase("null"))){
address1 = HtmlUtil.escape(rs.getString("address1"));
}

1 Comment

You really don't have to call and escape thrice, but that does look like my final code: if (address1 != null && address1 != "") address_tip += address1+"&#10;";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.