java - Reproduce NullPointerException on Stream.toList() method - Stack Overflow

时间: 2025-01-06 admin 业界

It go me figured on how to reproduce the NullPointerException (NPE) I saw in the logging.

The stacktrace points to the line of the Stream.toList() method in the findAll() method below. EventorRepostory is standard JpaRepository and toDomainModel is a lombok builder to a record class with exactly the same properties (non)-nullable.

What has got me so figured is that jpaRepository.findall() can only produce NPE safe list. The toDomainModel method is a lombok builder that should be also null safe unless it receives a null object. The first foolproofs the second, so how can the toList() can cause a NPE here?

public List<Eventor> findAll() {
    return eventorRepository
        .findAll()
        .parallelStream()
        .map(Eventor::toDomainModel)
        .toList();
}


@Entity
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Eventor {

@NotNull
private Integer id;

@Id
private UUID uuid;

private Instant creationDate;

private Location location; //class with @NotNull properties

@Embedded
private EventorState eventorState;
}

Running tests with properties null or objects null just runs e.g.

 @Test
public void test() {

    when(jpaRepository.findAll()).thenReturn(List.of(getEventor()));

    service.findAll().forEach(System.out::println);
}

{"timestamp":"2025-01-02T20:47:43.967Z","level":"ERROR","thread":"scheduling-1","logger":"org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler","message":"Unexpected exception occurred invoking async method: public void nu.eg.nu.business.services.EventorService.processEventor(java.util.Collection,java.time.Instant)","context":"default","exception":"java.lang.NullPointerException: null\n\tat java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(Unknown Source)\n\tat java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Unknown Source)\n\tat java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)\n\tat java.base/java.util.concurrent.ForkJoinTask.getThrowableException(Unknown Source)\n\tat java.base/java.util.concurrent.ForkJoinTask.reportException(Unknown Source)\n\tat java.base/java.util.concurrent.ForkJoinTask.invoke(Unknown Source)\n\tat java.base/java.util.stream.Nodes.collect(Unknown Source)\n\tat java.base/java.util.stream.ReferencePipeline.evaluateToNode(Unknown Source)\n\tat java.base/java.util.stream.AbstractPipeline.evaluate(Unknown Source)\n\tat java.base/java.util.stream.AbstractPipeline.evaluateToArrayNode(Unknown Source)\n\tat java.base/java.util.stream.ReferencePipeline.toArray(Unknown Source)\n\tat java.base/java.util.stream.ReferencePipeline.toArray(Unknown Source)\n\tat java.base/java.util.stream.ReferencePipeline.toList(Unknown Source)\n\tat nu.eg.numsi.persistence.EventorSqlRepository.findAll(EventorSqlRepository.java:26)\n\tat java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(Unknown Source)\n\tat java.base/java.lang.reflect.Method.invoke(Unknown Source)\n\tat org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:355)\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196)\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)\n\tat org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:768)\n\tat org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:138)\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)\n\tat org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:768)\n\tat org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:720)\n\tat nu.eg.nu.persistence.EventorSqlRepository$$SpringCGLIB$$0.findAll()\n\tat nu.eg.nu.business.services.EventorService.getAllEventor(EventorService.java:22)\n\tat nu.eg.nu.business.services.EventorService.getEventorAsMap(EventorService.java:26)\n\tat nu.eg.nu.business.services.EventorService.getEventorAsMap(EventorService.java:59)\n\tat nu.eg.nu.business.services.EventorService.processEventor(EventorService.java:34)\n\tat java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(Unknown Source)\n\tat java.base/java.lang.reflect.Method.invoke(Unknown Source)\n\tat org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:355)\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196)\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)\n\tat org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:768)\n\tat org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:379)\n\tat org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)\n\tat org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:768)\n\tat org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:113)\n\tat java.base/java.util.concurrent.FutureTask.run(Unknown Source)\n\tat java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)\n\tat java.base/java.lang.Thread.run(Unknown Source)\nCaused by: java.lang.NullPointerException: null\n"}

My hunch is that it could be the parrellelStream or the toList, but I want to reproduce it first.

最新文章