[英]java: weird NullPointerException in ternary operator (? : )
[英]Ternary Operator and unexpected NullPointerException
有时我从下面的行中获取NullPointerException
。
System.out.println("Date::"+ row != null ? row.getLegMaturityDate() : "null");
添加括号后,就可以了。
System.out.println("Date::"+ (row != null ? row.getLegMaturityDate() : "null"));
请澄清我的行为。 提前致谢。
"Date::" + row
从不为空,尽管row
有时为空。
也就是说, "Date::"+ row != null
等于("Date::"+ row) != null
,它始终为true。
这是运算符优先级的问题。 ChristofferHammarström的内容提要。 有关更多详细信息,请参见http://bmanolov.free.fr/javaoperators.php 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.