A utility method representing, “Both not null and

2019-08-05 01:35发布

String str = "abc";

Comparing this string variable like the following.

if(str.equals("abc")) {}

In case str is null, it will cause a java.lang.NullPointerException to be thrown as obvious.

To avoid that, an additional null check may be enforced. Such as,

if(str != null && str.equals("abc")) {}

I find it plain ugly. Better could be rewritten as follows.

if("abc".equals(str)) {}

This will never throw a java.lang.NullPointerException even though str is null. Besides, object equals null is never true.


The last case however, cannot be used, when the conditional expression is inverted like so,

if(!"abc".equals(str)) {
    System.out.println(str.length());
}

This will cause a java.lang.NullPointerException inside the if block, if str is null.

Can this somehow be avoided without rewriting the conditional statement like the following?

if(str != null && !"abc".equals(str)) {}

This is plain ugly and unreadable.


Although the example uses a String object, it may be a more complex object.

4条回答
做个烂人
2楼-- · 2019-08-05 02:13

An alternative could be to use the Java 8 optional wrapper

Optional<Customer> optional = findCustomer();

if (optional.isPresent()) {
Customer customer = maybeCustomer.get();
    ... use customer ...
}
else {
    ... deal with absence case ...
}

source: https://dzone.com/articles/java-8-optional-how-use-it

查看更多
Melony?
3楼-- · 2019-08-05 02:18

Long story short : There is simply no library method doing this which I know of. This if(str != null && !"abc".equals(str)) {} actually requires that both the objects to be compared are not null and not equal to each other.

A static utility method performing this task is sufficient to deal with.

/**
 * Returns {@code true} if and only if both the arguments (objects) are
 * <b>not</b> {@code null} and are not equal to each other and {@code false}
 * otherwise.
 *
 * @param a an object.
 * @param b an object to be compared with {@code a} for equality.
 * @return {@code true} if both the arguments (objects) are <b>not</b> {@code null}
 * and not equal to each other.
 */
public static boolean notEquals(Object a, Object b) {
    return (a == b || a == null || b == null) ? false : !a.equals(b);
}
查看更多
祖国的老花朵
4楼-- · 2019-08-05 02:30

You have to check for null at some point if you want to use str. There is simply no way around it. You can wrap this check into a additional utility function or something like this, but in the end you will not get around the additional check.

If you are a friend of using loads of additional libraries you could use org.apache.commons.lang.StringUtils#length(java.lang.String). That does just what you want, maybe you got a library like that present in your application anyway. The apache one is only a example. There are surely others around that do similar things.

If you want to remove the null check all together maybe the better question is: Why can str be null and it is possible to prevent it being null by not accepting this value from the very beginning.

查看更多
倾城 Initia
5楼-- · 2019-08-05 02:30

Another possible way to avoid nulls is using an assert: Look at this answer in another similar question:

How to check to see that a set of variables is not null before continuing

查看更多
登录 后发表回答