假设我有以下类层次结构:
Person对象包含Customer对象,Customer对象包含Address对象.
我不在乎客户是否有地址,但如果他们这样做,我想将其保存到数据库中.所以在这种情况下,拥有类似的东西是完美的:
try
{
Address addr = person.Customer.Address;
db.SaveAddress(addr);
}
catch
{
//I don't care, but if it is there, just save it.
}
Run Code Online (Sandbox Code Playgroud)
在上面,我不在乎Customer是null还是Address为null.我有另一种选择
if(person.Customer != null)
{
if(person.Customer.Address != null)
{
}
}
Run Code Online (Sandbox Code Playgroud)
如果层次结构很长,上述情况可能会很长.是否有更优雅的方法来检查对象链是否为null而不必检查每个对象.
我在Ruby工作,并发现需要在我的一些/大部分方法结束时使用条件返回语句.
这是我有的:
# <ident-list> -> [ident] <ident-A>
def ident_list(keys)
id = nil
ident_a_node = nil
## method hidden
return IdentifierListNode.new(id, ident_a_node) unless id.nil?
return nil
end
Run Code Online (Sandbox Code Playgroud)
有多种回报,是否有更好/更清洁的方式来解决这个问题?
假设我有10行代码.任何行都可能抛出NullPointerException.我不想关心这些例外.如果一行抛出异常,我希望执行者跳转到下一行并继续前进.我可以在Java上这样做吗?如果是的话,请给我一些示例代码.
更新:(添加样本来源以获得更清晰的问题)
first.setText(prize.first);
second.setText(prize.second);
third.setText(prize.third);
fourth.setText(prize.fourth);
fifth.setText(prize.fifth);
sixth.setText(prize.sixth);
seventh.setText(prize.seventh);
eighth.setText(prize.eighth);
Run Code Online (Sandbox Code Playgroud)
假设我上面有8行代码.我想要的是:如果第4行(或5,6,...)抛出异常,所有其他代码行都能正常工作.当然我可以使用try ... catch来逐行捕获异常.但这种方式使我的来源非常复杂.
关于空检查的编码标准我有疑问.我想知道它们之间的区别
if(a!=null)
Run Code Online (Sandbox Code Playgroud)
和
if(null!=a)
Run Code Online (Sandbox Code Playgroud)
哪一个更好,哪一个使用,为什么?
假设我有一个带三个参数的函数.当然,我可以通过这种方式检查此函数的任何两个参数是否为null.
returnType function(p1, p2, p3){
if((p1 == null && p2 == null) || (p2 == null && p3 == null) || (p3== null && p1 == null)){
return;
}
}
Run Code Online (Sandbox Code Playgroud)
但这相当麻烦,不能扩展到更多的参数.
这样做的优雅方式是什么?
提前致谢.
好吧,这是一个简单的问题,我没有任何问题,但我只是想确定.如果我做这样的事情,这是正确的吗?
if (person != null && person.getAge > 20) {...}
Run Code Online (Sandbox Code Playgroud)
我可以这样做吗?在同一行上进行null检查和对象访问?它是否正确?或者我必须在null检查语句上做一个内部语句,如:
if (person != null)
{
if (person.getAge() > 20) {...}
}
Run Code Online (Sandbox Code Playgroud)
imo,第一个声明不会抛出NPE,但我需要专业人士的反馈......
一般来说,我的问题是,对于带有&&的if语句,如果第一个布尔值为false,JVM是否会停止执行if语句,否则它将全部检查?
我在输入中有一些数据,我将不得不用它来设置POJO的所有属性.POJO可能部分设置.我的问题是仅在相关输入数据不为空时才设置属性.我知道我可以通过两种方式做到这一点:
if (input != null) {
obj.setData(input);
}
Run Code Online (Sandbox Code Playgroud)
要么
obj.setData(input != null ? input : obj.getData());
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种不那么难看的解决方案,对于具有大量属性的对象来说更好.
是否有更短的方法来检查字符串/对象是否为null?
String str = someMethodThatReturnsStringOrNull(someOthreObject);
if(str == null) { System.out.println("Empty"); }
Run Code Online (Sandbox Code Playgroud)
比如JS风格:
if(str) { System.out.println("Empty"); }
Run Code Online (Sandbox Code Playgroud) 当方法可以获得空引用而不是有效的对象值时,拥有像 IllegalArgumentException 这样的自定义异常并在所有情况下抛出它是否是个好主意?
public void method(String str){
if(str == null)throw new CustomIllegalArgumentException("str cannot be null");
}
Run Code Online (Sandbox Code Playgroud)
我认为这样我总能看到这种非法参数异常和其他 RuntimeExceptions 之间的区别。
这主意好不好?
PS:我看过像Avoiding != null statements这样的帖子
**更新:**所以我会知道这是程序员意图的异常,我会有清晰的日志。
java null android nullpointerexception nullreferenceexception
这是不好的做法还是任何性能损失,这是检查x不等于null
if( !(x == null) ){
System.out.println("x is not a null value");
}
Run Code Online (Sandbox Code Playgroud) 避免if用于nullJava检查的多个块的最佳方法是什么?
以下是我的示例代码.哪一个是最优化的方式?
if (address!=null} {
if (firstName!=null) {
if (lastName!=null) {
}
}
}
Run Code Online (Sandbox Code Playgroud) java ×9
android ×2
coding-style ×2
null ×2
function ×1
if-statement ×1
null-check ×1
parameters ×1
ruby ×1
syntax ×1