I tried to pass the Long value of an object to a method that expects a long primitive, the transfer works directly, unless the Long object is null. In this case, I get a Null Pointer exception.
Long foo=null;
bar.methodExpects_long_primitive(foo);
I can create a check if foo is null and skip the method call like
Long foo=null;
if(foo!=null){
bar.methodExpects_long_primitive(foo);
}
or if I want to provide a default value
Long foo=null;
bar.methodExpects_long_primitive(foo==null?defaultValue:foo);
Is there an elegant / best way to do this? I have to repeat this several times in the codebase and do it, it seems, adds a lot of conventions.
I could create my own method for this, but I would like to know if there is any such library method.
source
share