How to make a chained object null-safe for iteration [duplicate] - java

This question already has answers here:
Check chains of "get" calls for null
(11 answers)
Null check chain vs catching NullPointerException
(19 answers)
Closed 4 years ago.
New to Java.
I'm trying to avoid NullPointerException when iterating over a list. CollectionUtils.emptyIfNull() helps, but not for a chained object.
For example:
for (MyObject myObj : CollectionUtils.emptyIfNull(
result.item.getMyObject().getObjects())) {
// do stuff
}
I have no control over MyObject because I wrote a client that consumes this as a dependency. Any of these attributes could be null, and emptyIfNull() only protects against getObjects() returning null.
Is there a way to apply emptyIfNull() or something similar to the entire chain of calls?
Thanks for the help!

Related

Best way to check null for each part of an experssion in java [duplicate]

This question already has answers here:
Check chains of "get" calls for null
(11 answers)
Closed 1 year ago.
So I have something that looks like this:
(String) loginResponse.getGroup().getAdditionalProperties().get(something);
and each and every component of the expression could return null. I want to avoid throwing an exception and checking if null after each call doesn't look like the best way to do it:
temp= loginResponse.getGroup();
if(temp!=null){
temp= temp.getAdditionalProperties()
if(temp!=null){
temp.get(something)
}
}etc...
What's a better way to do this?
Thanks
you can use "Optional" and chain the call inside a map()
Optional.ofNullable(toto).map(A::getB).map(B::getC).orElseThrow(...)
Optional.ofNullable(toto).map(A::getB).map(B::getC).orElseGet(...)
You avoid nullPointerException by this way. It goes in the orElse... if anly element is null at any places

Null pointer exception inside Optional.ofNullable() [duplicate]

This question already has answers here:
How to convert Optional<Object> to Optional<String> [closed]
(3 answers)
Closed 2 years ago.
I am trying to convert a set to string inside Optional.OfNullable method like:
test.setAbc(Optional.ofNullable(rule.getSampleSet().toString()).orElse(null));
but if sampleSet is null it will give me a NullPointerException.
can anyone tell me how to resolve this issue using .map method with Optional?
I know one traditional way of doing it by checking nullability beforehand:
if(rule.getSampeSet != null)
but I am much interested in knowing if we can do it in one line.
Instead of calling toString() inside the ofNullable, you could map the optional to it:
test.setAbc(Optional.ofNullable(rule.getSampleSet()).map(Object::toString).orElse(null));

Avoid if for chained objects [duplicate]

This question already has answers here:
Avoiding NullPointerException in Java
(66 answers)
Closed 2 years ago.
I have one complex object just like this;
Object A
Object B
Object C
Object D
property A
property B
So If I need to show the property A in my view, I need to
A.getB().getC().getD().getPropertyA();
but what if my user doesn`t send the object C?
so I need to create one If for every object
if(A.getB() != null){
if(A.getB().getC() != null){
if(A.getB().getC().getD() != null){
//here I can show the propertyA
}
}
}
now I have to show this property in 3 views
There is a better way to does this? a framework or something like this to solve this problem?
You can use Optional.ofNullable and map:
Optional.ofNullable(A.GetB())
.map(B::getC)
.map(C::getD)
.map(D::getPropertyA)
.orElseThrow()
P.S.1: Some developers (and linters) find this approach a code smell, but I think it's more readable that way.
P.S.2: You can use a default value with .orElse instead of .orElseThrow.

Avoiding null check when accessing an object property in java [duplicate]

This question already has answers here:
Null check in Java 8 Elvis operator?
(2 answers)
Closed 5 years ago.
I have a situation where I need get the property of a Java object if the object exists or null.
Something like foo == null ? null : foo.bar
Is there an operator available in java to do the same?
No, there is no propagate null operator in Java, cf. C# for example, which does have one. (It was a proposal at some point in Java's evolution, but has not sadly yet been incorporated into the language: foo = foo?.bar would be an obvious notation.)
You need to write this out longhand, as you have done.

Java 8 method references for multiple statements in streams [duplicate]

This question already has answers here:
Multiple lambda method references
(3 answers)
Closed 5 years ago.
list.stream().forEach(e -> method(e)) can be converted to list.stream().forEach(this::method)
Similarly can we convert list.stream().forEach(e -> { method1(e); method2(e);}); using method references expressions. Big apologies if you don't understand question. I am using mobile app first time.
No you cannot.
The point of Method references in Java is to abstract (syntaxically) a lambda expression. Since forEach consumes a function that takes 1 element of type specified by the parent stream, there is no syntax sugar for double application using method references.
Even I'm not sure that this answer is wanted by you,
How about changing the method to static one in that class?

Categories

Resources