Java:如果结果为null,则如何分配到变量



这有效:

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

...但是我不想显示" null"。所以我尝试了这一点,但不起作用:

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

是使用此问题的最佳选择:?

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

使用三元运算符,您可以用一行代码分配一个值:

String address1 = (rs.getString("address1") != null) ?
                  HtmlUtil.escape(rs.getString("address1")) : "";
String address1 = HtmlUtil.escape(rs.getString("address1"));
if (address1 == null || address1.equals("null")) {
    address1 = "";
}
String address1 = HtmlUtil.escape(rs.getString("address1")) == null ? "" : HtmlUtil.escape(rs.getString("address1")); 

如果您想要一个丑陋的单线: - )

解决方案:

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

使用一个温度变量仅调用HtmlUtil.escape一次。然后检查null并分配给address1

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

尝试类似的东西

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

您可以尝试以下方法:

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

相关内容

  • 没有找到相关文章

最新更新