尝试在 Java 列表上进行 stream().filter().collect(Collectors.toList())) 时获取 NullPointerException
Getting NullPointerException when trying to stream().filter().collect(Collectors.toList())) on Java list
我在尝试搜索列表中的特定 Java 对象时遇到问题。
实际上我得到了我想要为另一个 Java class 工作的搜索功能。现在我尝试了另一个结果,但没有返回结果列表,而是得到了 NullPointerException。
我的函数是这样的:
public static List<Customer> searchByEverything(String keyword){
List<Customer> customerList = find.all(); //Using java Ebean
System.out.println(keyword); //Check if keyword is not empty
System.out.println(customerList); //Check if list is not empty
Predicate<Customer> customerPredicate = u -> u.name.toLowerCase().contains(keyword.toLowerCase());
try{
return customerList.stream().filter(customerPredicate).collect(Collectors.toList());
}catch (Exception e){
e.printStackTrace();
}
这是来自异常 e:
的 strack 跟踪
java.lang.NullPointerException
at models.Customer.lambda$searchByEverything(Customer.java:174)
at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:174)
at java.util.ArrayList$Itr.forEachRemaining(ArrayList.java:891)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at models.Customer.searchByEverything(Customer.java:179)
at controllers.Application.searchCustomers(Application.java:262)
at router.Routes$$anonfun$routes$$anonfun$applyOrElse$$anonfun$apply.apply(Routes.scala:479)
at router.Routes$$anonfun$routes$$anonfun$applyOrElse$$anonfun$apply.apply(Routes.scala:479)
at play.core.routing.HandlerInvokerFactory$$anon.resultCall(HandlerInvoker.scala:157)
at play.core.routing.HandlerInvokerFactory$$anon.resultCall(HandlerInvoker.scala:156)
at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$$anon$$anon.invocation(HandlerInvoker.scala:136)
at play.core.j.JavaAction$$anon.call(JavaAction.scala:73)
at play.http.HttpRequestHandler.call(HttpRequestHandler.java:54)
at play.mvc.Security$AuthenticatedAction.call(Security.java:53)
at play.core.j.JavaAction$$anonfun.apply(JavaAction.scala:108)
at play.core.j.JavaAction$$anonfun.apply(JavaAction.scala:108)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1(Future.scala:24)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
at play.core.j.HttpExecutionContext$$anon.run(HttpExecutionContext.scala:56)
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:70)
at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:48)
at scala.concurrent.impl.Future$.apply(Future.scala:31)
at scala.concurrent.Future$.apply(Future.scala:492)
at play.core.j.JavaAction.apply(JavaAction.scala:108)
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:112)
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:112)
at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:111)
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:110)
at scala.Option.map(Option.scala:146)
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:110)
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:103)
at scala.concurrent.Future$$anonfun$flatMap.apply(Future.scala:251)
at scala.concurrent.Future$$anonfun$flatMap.apply(Future.scala:249)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run.apply$mcV$sp(BatchingExecutor.scala:91)
at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run.apply(BatchingExecutor.scala:91)
at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run.apply(BatchingExecutor.scala:91)
at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:72)
at akka.dispatch.BatchingExecutor$BlockableBatch.run(BatchingExecutor.scala:90)
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:39)
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:415)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
正如我所说,我对不同的 class 实施了完全相同的搜索,而且效果非常好。 customerList
和 keyword
都不为空。在我的 customerList
和另一个列表中搜索的唯一区别是它有大约 4500 个对象,每个对象有大约 50 个属性。也许这就是问题所在?
提前致谢!
很可能 u.name
是 null
尝试在
中改变您的 Predicate
Predicate<Customer> customerPredicate = u -> (u.name != null && u.name.toLowerCase().contains(keyword.toLowerCase()));
要查看具有 name == null
的 Customer 实例是什么,您可以在您的链中添加一个 peek
方法。
customerList.stream()
.peek(c -> {
if (c.name == null) {
// do something...
}
}).filter(customerPredicate).collect(Collectors.toList());
我在尝试搜索列表中的特定 Java 对象时遇到问题。
实际上我得到了我想要为另一个 Java class 工作的搜索功能。现在我尝试了另一个结果,但没有返回结果列表,而是得到了 NullPointerException。
我的函数是这样的:
public static List<Customer> searchByEverything(String keyword){
List<Customer> customerList = find.all(); //Using java Ebean
System.out.println(keyword); //Check if keyword is not empty
System.out.println(customerList); //Check if list is not empty
Predicate<Customer> customerPredicate = u -> u.name.toLowerCase().contains(keyword.toLowerCase());
try{
return customerList.stream().filter(customerPredicate).collect(Collectors.toList());
}catch (Exception e){
e.printStackTrace();
}
这是来自异常 e:
的 strack 跟踪java.lang.NullPointerException
at models.Customer.lambda$searchByEverything(Customer.java:174)
at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:174)
at java.util.ArrayList$Itr.forEachRemaining(ArrayList.java:891)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at models.Customer.searchByEverything(Customer.java:179)
at controllers.Application.searchCustomers(Application.java:262)
at router.Routes$$anonfun$routes$$anonfun$applyOrElse$$anonfun$apply.apply(Routes.scala:479)
at router.Routes$$anonfun$routes$$anonfun$applyOrElse$$anonfun$apply.apply(Routes.scala:479)
at play.core.routing.HandlerInvokerFactory$$anon.resultCall(HandlerInvoker.scala:157)
at play.core.routing.HandlerInvokerFactory$$anon.resultCall(HandlerInvoker.scala:156)
at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$$anon$$anon.invocation(HandlerInvoker.scala:136)
at play.core.j.JavaAction$$anon.call(JavaAction.scala:73)
at play.http.HttpRequestHandler.call(HttpRequestHandler.java:54)
at play.mvc.Security$AuthenticatedAction.call(Security.java:53)
at play.core.j.JavaAction$$anonfun.apply(JavaAction.scala:108)
at play.core.j.JavaAction$$anonfun.apply(JavaAction.scala:108)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1(Future.scala:24)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
at play.core.j.HttpExecutionContext$$anon.run(HttpExecutionContext.scala:56)
at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:70)
at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:48)
at scala.concurrent.impl.Future$.apply(Future.scala:31)
at scala.concurrent.Future$.apply(Future.scala:492)
at play.core.j.JavaAction.apply(JavaAction.scala:108)
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:112)
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply$$anonfun$apply.apply(Action.scala:112)
at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:111)
at play.api.mvc.Action$$anonfun$apply$$anonfun$apply.apply(Action.scala:110)
at scala.Option.map(Option.scala:146)
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:110)
at play.api.mvc.Action$$anonfun$apply.apply(Action.scala:103)
at scala.concurrent.Future$$anonfun$flatMap.apply(Future.scala:251)
at scala.concurrent.Future$$anonfun$flatMap.apply(Future.scala:249)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run.apply$mcV$sp(BatchingExecutor.scala:91)
at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run.apply(BatchingExecutor.scala:91)
at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run.apply(BatchingExecutor.scala:91)
at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:72)
at akka.dispatch.BatchingExecutor$BlockableBatch.run(BatchingExecutor.scala:90)
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:39)
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:415)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
正如我所说,我对不同的 class 实施了完全相同的搜索,而且效果非常好。 customerList
和 keyword
都不为空。在我的 customerList
和另一个列表中搜索的唯一区别是它有大约 4500 个对象,每个对象有大约 50 个属性。也许这就是问题所在?
提前致谢!
很可能 u.name
是 null
尝试在
中改变您的Predicate
Predicate<Customer> customerPredicate = u -> (u.name != null && u.name.toLowerCase().contains(keyword.toLowerCase()));
要查看具有 name == null
的 Customer 实例是什么,您可以在您的链中添加一个 peek
方法。
customerList.stream()
.peek(c -> {
if (c.name == null) {
// do something...
}
}).filter(customerPredicate).collect(Collectors.toList());