首页 > 解决方案 > Java 三元运算符错误地评估 null

问题描述

今天我在写测试时遇到了一个奇怪的情况。基本上,我有一个数据类。以 Toy 为例,我们可以从中检索名称:

public class Toy {

    private String name;

    public Toy(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}

我有一个例外,它的工作方式与此类似(例如,在它变坏之前只显示我们正在处理的所有对象的数据);我还包括一个主要用于测试目的:

public class ToyFactoryException extends Exception {

    public ToyFactoryException(Toy firstToy, Toy secondToy) {
            super("An error occurred when manufacturing: " + 
                       "\nfirstToy: " + firstToy != null ? firstToy.getName() : null + 
                       "\nsecondToy: " + secondToy != null ? secondToy.getName() : null);
        }

    public static void main(String[] args) {
        try {

            throw new ToyFactoryException(null, new Toy("hi"));

        } catch (ToyFactoryException myException) {

            System.out.println("It should be there.");

        } catch (Exception exception) {

            System.out.println("But it's there instead.");

        }
    }

}

正如我在第一个 catch 块中所写,异常应该在 ToyFactoryException 中被捕获。

但是,在例外情况下,它试图在这里读取 firstToy.getName() :firstToy != null ? firstToy.getName() : null

firstToy != null应该评估为假,这意味着它不应该首先尝试调用firstToy.getName()。当您以相反的顺序编写它时:

public ToyFactoryException(Toy firstToy, Toy secondToy) {
    super("An error occurred when manufacturing: " + 
               "\nfirstToy: " + firstToy != null ? null : firstToy.getName() + 
               "\nsecondToy: " + secondToy != null ? secondToy.getName() : null);
        }

你意识到它null现在改为阅读,这意味着它真的阅读firstToy != null为真的。

如果你改用这种方式编写 main (null 是构造函数的第二个参数):

public static void main(String[] args) {
    try {

        throw new ToyFactoryException(new Toy("hi"), null);

    } catch (ToyFactoryException myException) {

        System.out.println("It should be there.");

    } catch (Exception exception) {

        System.out.println("But it's there instead.");

    }
}

尽管 secondToy 三元条件的编写方式与 firstToy 三元相同,但它工作正常。

为什么 firstToy 上的三元条件不能正确评估 null?

标签: javaternary-operator

解决方案


您应该在条件表达式周围加上括号。

这个:

"string " + firstToy != null ? firstToy.getName() : null

这意味着:

("string " + firstToy) != null ? firstToy.getName() : null

你需要这个:

"string " + (firstToy != null ? firstToy.getName() : null) 

推荐阅读