繁体   English   中英

三元运算符和意外的NullPointerException

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM