java - Reproduce NullPointerException on Stream.toList() method - Stack Overflow
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.
- 紫光集团两年内豪掷90亿美元
- 软件定义网络:正在进行的网络变革
- 奇虎诉腾讯索赔1.5亿创天价 双方股价昨齐上涨
- c++ - Why is my OpenGL application rendering a 3D model with unexpected transparency? - Stack Overflow
- How to solve the error in "Plugin [id: 'org.jetbrains.kotlin.android', version: '1.9.0'
- angularjs - Laravel Custom Auth in registering - Stack Overflow
- reactjs - Why can't AWS Amplify find my components from a working version of my React App? - Stack Overflow
- flutter - App Name Not Updating in Android Recent Apps View Despite Manifest and Strings Configuration - Stack Overflow
- ios - CocoaPods could not find compatible versions for pod "FirebaseCore": - Stack Overflow
- why docker change value of args if put in FROM - Stack Overflow
- python - Converting Document Docx with Comments to markit using markitdown - Stack Overflow
- The blender. How to combine multiple textures into one - Stack Overflow
- amazon web services - Problem with eventbridge when scheduling a rule - Stack Overflow
- Power query performance (SOAP connector) very low on migrating from Excel to Power BI Desktop - Stack Overflow
- javascript - Why Does Putting a Custom HTML Element Inside Another Leaves the Second Hidden? - Stack Overflow
- ios - Unit Test Cases Fail with "Message from debugger: killed" on Xcode 16.x but Work Fine on Xcode 15 - Stac
- node.js - Nestjs can't find build source after creating workspace - Stack Overflow