java.lang.NullPointerException

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • In our application we use EntityManager.Clear in a method annotated with @transactional although we know it's not best practice ( we'll remove the call to clear()). when performing the same operation in large scale (the operation is done on single element and not in a robust way), we get NullPointerException. stack trace: {code:java} java.lang.NullPointerException at org.hibernate.engine.internal.StatefulPersistenceContext.clear(StatefulPersistenceContext.java:237) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] at org.hibernate.internal.SessionImpl.internalClear(SessionImpl.java:340) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] at org.hibernate.internal.SessionImpl.clear(SessionImpl.java:336) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] at org.hibernate.jpa.spi.AbstractEntityManagerImpl.clear(AbstractEntityManagerImpl.java:1419) ~[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final] at sun.reflect.GeneratedMethodAccessor178.invoke(Unknown Source) ~[?:?] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_60] at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_60] at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344) ~[spring-orm-4.2.3.RELEASE.jar:4.2.3.RELEASE] at com.sun.proxy.$Proxy79.clear(Unknown Source) ~[?:?] {code} the stack trace show that the the source of the call is our call to entityManager.clear().
    via by Meytal,
  • In our application we use EntityManager.Clear in a method annotated with @transactional although we know it's not best practice ( we'll remove the call to clear()). when performing the same operation in large scale (the operation is done on single element and not in a robust way), we get NullPointerException. stack trace: {code:java} java.lang.NullPointerException at org.hibernate.engine.internal.StatefulPersistenceContext.clear(StatefulPersistenceContext.java:237) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] at org.hibernate.internal.SessionImpl.internalClear(SessionImpl.java:340) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] at org.hibernate.internal.SessionImpl.clear(SessionImpl.java:336) ~[hibernate-core-4.3.8.Final.jar:4.3.8.Final] at org.hibernate.jpa.spi.AbstractEntityManagerImpl.clear(AbstractEntityManagerImpl.java:1419) ~[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final] at sun.reflect.GeneratedMethodAccessor178.invoke(Unknown Source) ~[?:?] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_60] at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_60] at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344) ~[spring-orm-4.2.3.RELEASE.jar:4.2.3.RELEASE] at com.sun.proxy.$Proxy79.clear(Unknown Source) ~[?:?] {code} the stack trace show that the the source of the call is our call to entityManager.clear().
    via by Meytal,
  • I have encountered {{NullPointerException}} exceptions coming from Hibernate's internals: {code:none} java.lang.NullPointerException at org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:138) at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:74) at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:44) at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1264) {code} {code:none} java.lang.NullPointerException at org.hibernate.engine.internal.StatefulPersistenceContext.clear(StatefulPersistenceContext.java:226) at org.hibernate.internal.SessionImpl.cleanup(SessionImpl.java:566) at org.hibernate.internal.SessionImpl.close(SessionImpl.java:438) {code} I was doing something like the following in a (stateful) {{Session}}: {code:java} final Set<Object> children = parent.getChildren(); final Set<Object> childrenCopy = new HashSet<>(children); for (final Object child : children) { children.remove(child); session.delete(child); } session.flush(); for (final Object child : childrenCopy) { session.evict(child); } session.commit(); {code} Lazy attribute loading is enabled with the following {{hibernate-enhance-maven-plugin}} parameters, but the involved {{@OneToMany}} relation doesn't have to be {{FetchType.LAZY}}. {code:xml} <configuration> <enableLazyInitialization>true</enableLazyInitialization> <enableDirtyTracking>false</enableDirtyTracking> <enableAssociationManagement>false</enableAssociationManagement> <enableExtendedEnhancement>false</enableExtendedEnhancement> </configuration> {code} Removing the {{evict()}} call resolves the issue. Both places where the exception occured was calling [{{StatefulPersistenceContext.reentrantSafeEntityEntries()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java#L1121] internally, which returns an array by calling [{{EntityEntryContext.reentrantSafeEntityEntries()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L243]. After evicting an entity, this array contained _only_ {{null}} values (instead of {{EntityEntryContext.EntityEntryCrossRefImpl}} instances), and calling [{{Map.Entry.getValue()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java#L138] or [{{Map.Entry.getKey()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java#L226] on these {{null}} values resulted in the {{NullPointerException}} errors. By looking at the source code of [{{EntityEntryContext.reentrantSafeEntityEntries()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L243], I can see that it creates a new array of size {{count}}, and fills the items of this new array by walking the {{ManagedEntity}} linked list from {{head}} by calling {{$$_hibernate_getNextManagedEntity()}}. However, {{count}} is non-zero and {{head}} is null here, so we get an array of {{count}} {{null}} values. And by debugging [{{DefaultEvictEventListener.onEvict()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultEvictEventListener.java#L46], I could see that it reaches [line 89|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultEvictEventListener.java#L89] without throwing an (ignored) exception there. (The [{{doEvict()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultEvictEventListener.java#L101] method is _not_ called in this code path.) Only the [{{StatefulPersistenceContext.removeEntry()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java#L437] method is called, which in turn calls [{{EntityEntryContext.removeEntityEntry()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L168]. Debugging this [{{EntityEntryContext.removeEntityEntry()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L168] method shows that the argument is an instance of {{ManagedEntity}} in [line 174|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L174] (because of the bytecode enhancement), but in lines [189|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L189] and [190|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L190], its {{$$_hibernate_getPreviousManagedEntity()}} and {{$$_hibernate_getNextManagedEntity()}} methods return {{null}} (its bytecode enhanced {{$$_hibernate_previousManagedEntity}} and {{$$_hibernate_nextManagedEntity}} fields were also {{null}} upon inspection). So in lines [210|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L210] and [219|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L219], the {{head}} and {{tail}} variables are set to {{null}} (the {{assert}} operations would also fail). So in the subsequent call of [{{reentrantSafeEntityEntries()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L243] runs with a {{null}} {{head}} and non-zero {{count}}, causing the {{NullPointerException}} errors. I couldn't figure out why this child entity had {{null}} values for the {{$$_hibernate_previousManagedEntity}}, {{$$_hibernate_nextManagedEntity}} and {{$$_hibernate_entityEntryHolder}} fields, but this might be the cause of the exceptions. Other managed entities of the session (including the parent entity) had non-null values. Setting the {{enableLazyInitialization}} parameter of the {{hibernate-enhance-maven-plugin}} to {{false}} also resolves the problem.
    via by Gábor Varga,
  • I have encountered {{NullPointerException}} exceptions coming from Hibernate's internals: {code:none} java.lang.NullPointerException at org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:138) at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:74) at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:44) at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1264) {code} {code:none} java.lang.NullPointerException at org.hibernate.engine.internal.StatefulPersistenceContext.clear(StatefulPersistenceContext.java:226) at org.hibernate.internal.SessionImpl.cleanup(SessionImpl.java:566) at org.hibernate.internal.SessionImpl.close(SessionImpl.java:438) {code} I was doing something like the following in a (stateful) {{Session}}: {code:java} final Set<Object> children = parent.getChildren(); final Set<Object> childrenCopy = new HashSet<>(children); for (final Object child : children) { children.remove(child); session.delete(child); } session.flush(); for (final Object child : childrenCopy) { session.evict(child); } session.commit(); {code} Lazy attribute loading is enabled with the following {{hibernate-enhance-maven-plugin}} parameters, but the involved {{@OneToMany}} relation doesn't have to be {{FetchType.LAZY}}. {code:xml} <configuration> <enableLazyInitialization>true</enableLazyInitialization> <enableDirtyTracking>false</enableDirtyTracking> <enableAssociationManagement>false</enableAssociationManagement> <enableExtendedEnhancement>false</enableExtendedEnhancement> </configuration> {code} Removing the {{evict()}} call resolves the issue. Both places where the exception occured was calling [{{StatefulPersistenceContext.reentrantSafeEntityEntries()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java#L1121] internally, which returns an array by calling [{{EntityEntryContext.reentrantSafeEntityEntries()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L243]. After evicting an entity, this array contained _only_ {{null}} values (instead of {{EntityEntryContext.EntityEntryCrossRefImpl}} instances), and calling [{{Map.Entry.getValue()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java#L138] or [{{Map.Entry.getKey()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java#L226] on these {{null}} values resulted in the {{NullPointerException}} errors. By looking at the source code of [{{EntityEntryContext.reentrantSafeEntityEntries()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L243], I can see that it creates a new array of size {{count}}, and fills the items of this new array by walking the {{ManagedEntity}} linked list from {{head}} by calling {{$$_hibernate_getNextManagedEntity()}}. However, {{count}} is non-zero and {{head}} is null here, so we get an array of {{count}} {{null}} values. And by debugging [{{DefaultEvictEventListener.onEvict()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultEvictEventListener.java#L46], I could see that it reaches [line 89|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultEvictEventListener.java#L89] without throwing an (ignored) exception there. (The [{{doEvict()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultEvictEventListener.java#L101] method is _not_ called in this code path.) Only the [{{StatefulPersistenceContext.removeEntry()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java#L437] method is called, which in turn calls [{{EntityEntryContext.removeEntityEntry()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L168]. Debugging this [{{EntityEntryContext.removeEntityEntry()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L168] method shows that the argument is an instance of {{ManagedEntity}} in [line 174|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L174] (because of the bytecode enhancement), but in lines [189|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L189] and [190|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L190], its {{$$_hibernate_getPreviousManagedEntity()}} and {{$$_hibernate_getNextManagedEntity()}} methods return {{null}} (its bytecode enhanced {{$$_hibernate_previousManagedEntity}} and {{$$_hibernate_nextManagedEntity}} fields were also {{null}} upon inspection). So in lines [210|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L210] and [219|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L219], the {{head}} and {{tail}} variables are set to {{null}} (the {{assert}} operations would also fail). So in the subsequent call of [{{reentrantSafeEntityEntries()}}|https://github.com/hibernate/hibernate-orm/blob/5.1.0/hibernate-core/src/main/java/org/hibernate/engine/internal/EntityEntryContext.java#L243] runs with a {{null}} {{head}} and non-zero {{count}}, causing the {{NullPointerException}} errors. I couldn't figure out why this child entity had {{null}} values for the {{$$_hibernate_previousManagedEntity}}, {{$$_hibernate_nextManagedEntity}} and {{$$_hibernate_entityEntryHolder}} fields, but this might be the cause of the exceptions. Other managed entities of the session (including the parent entity) had non-null values. Setting the {{enableLazyInitialization}} parameter of the {{hibernate-enhance-maven-plugin}} to {{false}} also resolves the problem.
    via by Gábor Varga,
    • java.lang.NullPointerException at org.hibernate.engine.internal.StatefulPersistenceContext.clear(StatefulPersistenceContext.java:237)[hibernate-core-4.3.8.Final.jar:4.3.8.Final] at org.hibernate.internal.SessionImpl.internalClear(SessionImpl.java:340)[hibernate-core-4.3.8.Final.jar:4.3.8.Final] at org.hibernate.internal.SessionImpl.clear(SessionImpl.java:336)[hibernate-core-4.3.8.Final.jar:4.3.8.Final] at org.hibernate.jpa.spi.AbstractEntityManagerImpl.clear(AbstractEntityManagerImpl.java:1419)[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final] at sun.reflect.GeneratedMethodAccessor178.invoke(Unknown Source)[?:?] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[?:1.8.0_60] at java.lang.reflect.Method.invoke(Method.java:497)[?:1.8.0_60] at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344)[spring-orm-4.2.3.RELEASE.jar:4.2.3.RELEASE] at com.sun.proxy.$Proxy79.clear(Unknown Source)[?:?]
    No Bugmate found.