Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
Edit the question to include
desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem
. This will help others answer the question.
Closed
1 year ago
.
I made a filter for a list of Objects and searching for the name of one objects in this list:
tables.getTable().stream().filter(p->p.geName().equals(referenceName)).findAny;
The
referenceName
is not
null
and also the object that contains this name exist in the list, but it results an error, here is the result:
java.lang.NullPointerException at
comparator.complex.obj.comparison.XmlObjComparison.lambda$XmlObjectsComparison$8(XmlObjComparison.java:359)
java.base/java.util.stream.MatchOps$1MatchSink.accept(MatchOps.java:90)
java.base/java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1632)
java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:127)
java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:502)
java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:488)
java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
java.base/java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:230)
java.base/java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:196)
java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
java.base/java.util.stream.ReferencePipeline.noneMatch(ReferencePipeline.java:538)
comparator.complex.obj.comparison.XmlObjComparison.XmlObjectsComparison(XmlObjComparison.java:359)
comparator.complex.obj.comparison.XmlObjComparison.test2(XmlObjComparison.java:584)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566) at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at
org.junit.runners.ParentRunner.run(ParentRunner.java:363) at
org.junit.runner.JUnitCore.run(JUnitCore.java:137) at
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
what is possible? because in some cases it works and in some others it does not work!
–
You should use
Stream::peek
to debug the application and use additional filters or null check:
tables.getTable() // some collection
.stream()
.peek(p -> System.out.println("1: p=" + Objects.toString(p))) //
.filter(Objects::nonNull) // make sure p is not null
.peek(p -> System.out.println("2: p.name=" + Objects.toString(p.getName()))) //
.filter(p -> Objects.equals(referenceName, p.getName()))
.findAny();
Mere null check
tables.getTable() // some collection
.stream()
.filter(p -> p != null && Objects.equals(referenceName, p.getName()))
.findAny();
tables.getTable().stream().filter(p->p.geName().equals(referenceName)).findAny;
It must be p.geName()
returning null. Try either Objects.equals(referenceName, p.geName())
or referenceName.equals(p.geName())
or put in a null check. Also, I assume that findAny
is a method call but you excluded the parenthesis. Just to clarify: I'm not saying that p
is null, but that the result of p.geName()
might be null. If it is, then calling .equals()
on it will obviously yield a NullPointerException
.
If your problem is still not solved, you're going to have to provide more information, like how you are creating the stream, what you are trying to do, what the p
variable even is and the structure of p
's class.
–