リスト内の特定のJavaオブジェクトを検索しようとしたときに問題が発生しました。
実際に私は別のJava=クラスで機能したい検索関数を取得しました。今度は別のクラスで試してみましたが、結果のリストを返す代わりに、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からの追跡トレースです。
Java.lang.NullPointerException
at models.Customer.lambda$searchByEverything$2(Customer.Java:174)
at Java.util.stream.ReferencePipeline$2$1.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$1$$anonfun$applyOrElse$17$$anonfun$apply$17.apply(Routes.scala:479)
at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$17$$anonfun$apply$17.apply(Routes.scala:479)
at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:157)
at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:156)
at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$14$$anon$3$$anon$1.invocation(HandlerInvoker.scala:136)
at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:73)
at play.http.HttpRequestHandler$1.call(HttpRequestHandler.Java:54)
at play.mvc.Security$AuthenticatedAction.call(Security.Java:53)
at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:108)
at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:108)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
at play.core.j.HttpExecutionContext$$anon$2.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$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:112)
at play.api.mvc.Action$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:112)
at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
at play.api.mvc.Action$$anonfun$apply$2$$anonfun$apply$5.apply(Action.scala:111)
at play.api.mvc.Action$$anonfun$apply$2$$anonfun$apply$5.apply(Action.scala:110)
at scala.Option.map(Option.scala:146)
at play.api.mvc.Action$$anonfun$apply$2.apply(Action.scala:110)
at play.api.mvc.Action$$anonfun$apply$2.apply(Action.scala:103)
at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:251)
at scala.concurrent.Future$$anonfun$flatMap$1.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$1.apply$mcV$sp(BatchingExecutor.scala:91)
at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run$1.apply(BatchingExecutor.scala:91)
at akka.dispatch.BatchingExecutor$BlockableBatch$$anonfun$run$1.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)
私が言ったように、私は別のクラスにまったく同じ検索を実装していて、それは完全にうまくいきます。 customerList
とkeyword
の両方が空ではありません。私のcustomerList
と他のリストの検索の唯一の違いは、オブジェクトごとに約50の属性を持つ約4500のオブジェクトがあることです。多分それが問題ですか?
前もって感謝します!
可能性が高い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());