return super.isAvailable() && expander != null
&& rightNotLeft ? !expander.isExpandedRight() : expander.isExpandedRight();
我的问题是,当expandder为null时,我得到一个空指针异常。但我并不认为这应该发生,因为expandder!= null被评估为false,因为正在使用ANDs,整个表达式应该短路并返回false。
return super.isAvailable() && expander != null
&& (rightNotLeft ? !expander.isExpandedRight() : expander.isExpandedRight());
上面的代码(添加括号)解决了这个问题。然而这对我来说没有意义,因为无论条件运算符发生了什么,都无法返回true,所以它不应该短路吗?
感谢您的回复。
答案 0 :(得分:1)
这是由于operator precedence。没有明确括号的条件实际上被评估为
return (super.isAvailable() && expander != null
&& rightNotLeft) ? !expander.isExpandedRight() : expander.isExpandedRight();