java.lang.NullPointerException

Hibernate JIRA | Gábor Varga | 12 months ago
tip
Your exception is missing from the Samebug knowledge base.
Here are the best solutions we found on the Internet.
Click on the to mark the helpful solution and get rewards for you help.
  1. 0

    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.

    Hibernate JIRA | 12 months ago | Gábor Varga
    java.lang.NullPointerException
  2. 0

    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.

    Hibernate JIRA | 12 months ago | Gábor Varga
    java.lang.NullPointerException

    Root Cause Analysis

    1. java.lang.NullPointerException

      No message provided

      at org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes()
    2. Hibernate
      SessionImpl.autoFlushIfRequired
      1. org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:138)
      2. org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:74)
      3. org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:44)
      4. org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1264)
      4 frames