java.lang.NullPointerException

Hibernate JIRA | jason zhang | 1 decade ago
  1. 0

    I do not have this problem before when I use hibernate 2.0. yesterday I changed to 2.1 beta 3 and used <idbag>, I got this exception. This problem may be related to <idbag>. The exception is throwed just after the email is inserted. The email is a <idbag> mapped. Hibernate: insert into email (contactID, id, email, type) values (?, ?, ?, ?) Hibernate: select seq_email.nextval from dual java.lang.NullPointerException at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22) at net.sf.hibernate.collection.CollectionPersister.recreate(CollectionPersister.java:705) at net.sf.hibernate.impl.ScheduledCollectionRecreate.execute(ScheduledCollectionRecreate.java:22) at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2278) at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2238) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2178) at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:56) at com.access.sync.framework.persistence.HibernatePersister.commit(HibernatePersister.java:311) at com.access.sync.framework.persistence.test.ContactPersisterTester.myTestSave(ContactPersisterTester.java:126) at com.access.sync.framework.persistence.test.ContactPersisterTester.testAll(ContactPersisterTester.java:79) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at junit.framework.TestCase.runTest(TestCase.java:154) at junit.framework.TestCase.runBare(TestCase.java:127) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at junit.framework.TestSuite.runTest(TestSuite.java:208) at junit.framework.TestSuite.run(TestSuite.java:203) at junit.textui.TestRunner.doRun(TestRunner.java:116) at junit.textui.TestRunner.doRun(TestRunner.java:109) at junit.textui.TestRunner.run(TestRunner.java:72) at junit.textui.TestRunner.run(TestRunner.java:57)

    Hibernate JIRA | 1 decade ago | jason zhang
    java.lang.NullPointerException
  2. 0

    I do not have this problem before when I use hibernate 2.0. yesterday I changed to 2.1 beta 3 and used <idbag>, I got this exception. This problem may be related to <idbag>. The exception is throwed just after the email is inserted. The email is a <idbag> mapped. Hibernate: insert into email (contactID, id, email, type) values (?, ?, ?, ?) Hibernate: select seq_email.nextval from dual java.lang.NullPointerException at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22) at net.sf.hibernate.collection.CollectionPersister.recreate(CollectionPersister.java:705) at net.sf.hibernate.impl.ScheduledCollectionRecreate.execute(ScheduledCollectionRecreate.java:22) at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2278) at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2238) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2178) at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:56) at com.access.sync.framework.persistence.HibernatePersister.commit(HibernatePersister.java:311) at com.access.sync.framework.persistence.test.ContactPersisterTester.myTestSave(ContactPersisterTester.java:126) at com.access.sync.framework.persistence.test.ContactPersisterTester.testAll(ContactPersisterTester.java:79) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at junit.framework.TestCase.runTest(TestCase.java:154) at junit.framework.TestCase.runBare(TestCase.java:127) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at junit.framework.TestSuite.runTest(TestSuite.java:208) at junit.framework.TestSuite.run(TestSuite.java:203) at junit.textui.TestRunner.doRun(TestRunner.java:116) at junit.textui.TestRunner.doRun(TestRunner.java:109) at junit.textui.TestRunner.run(TestRunner.java:72) at junit.textui.TestRunner.run(TestRunner.java:57)

    Hibernate JIRA | 1 decade ago | jason zhang
    java.lang.NullPointerException
  3. 0

    When using select-before-update with an entity that does not have an id field declared, saving the entity will result in a NullPointerException: java.lang.NullPointerException at net.sf.hibernate.persister.AbstractEntityPersister.getCurrentPersistentState(AbstractEntityPersister.java:1249) at net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2475) at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2422) at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2224) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2203) at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61) at org.springframework.orm.hibernate.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:386) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:314) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:189) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:134) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:148) at $Proxy0.saveCustomer(Unknown Source) ... The fix is to verify that an object is versioned before trying to add the version field as a PreparedStatement parameter: --- AbstractEntityPersister.java.old 2004-04-21 14:15:30.000000000 -0400 +++ AbstractEntityPersister.java 2004-04-21 14:16:53.000000000 -0400 @@ -1246,7 +1246,9 @@ ResultSet rs = null; try { getIdentifierType().nullSafeSet(ps, id, 1, session); - getVersionType().nullSafeSet( ps, version, getIdentifierColumnNames().length+1, session ); + if ( isVersioned() ) { + getVersionType().nullSafeSet( ps, version, getIdentifierColumnNames().length+1, session ); + } rs = session.getBatcher().getResultSet(ps); if ( !rs.next() ) throw new StaleObjectStateException( getMappedClass(), id ); for (int i=0; i<types.length; i++) {

    Hibernate JIRA | 1 decade ago | Patrick Peralta
    java.lang.NullPointerException
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    When using select-before-update with an entity that does not have an id field declared, saving the entity will result in a NullPointerException: java.lang.NullPointerException at net.sf.hibernate.persister.AbstractEntityPersister.getCurrentPersistentState(AbstractEntityPersister.java:1249) at net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2475) at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2422) at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2224) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2203) at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61) at org.springframework.orm.hibernate.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:386) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:314) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:189) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:134) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:148) at $Proxy0.saveCustomer(Unknown Source) ... The fix is to verify that an object is versioned before trying to add the version field as a PreparedStatement parameter: --- AbstractEntityPersister.java.old 2004-04-21 14:15:30.000000000 -0400 +++ AbstractEntityPersister.java 2004-04-21 14:16:53.000000000 -0400 @@ -1246,7 +1246,9 @@ ResultSet rs = null; try { getIdentifierType().nullSafeSet(ps, id, 1, session); - getVersionType().nullSafeSet( ps, version, getIdentifierColumnNames().length+1, session ); + if ( isVersioned() ) { + getVersionType().nullSafeSet( ps, version, getIdentifierColumnNames().length+1, session ); + } rs = session.getBatcher().getResultSet(ps); if ( !rs.next() ) throw new StaleObjectStateException( getMappedClass(), id ); for (int i=0; i<types.length; i++) {

    Hibernate JIRA | 1 decade ago | Patrick Peralta
    java.lang.NullPointerException
  6. 0

    http://forum.hibernate.org/viewtopic.php?t=930720&sid=26f2eff17aaf6cda39dbf0d9dab823a9 Replicating an object with a many-to-many set while using native generated ids causes the following exception: java.lang.NullPointerException: null key for collection: edu.academyart.model.B.setOfAs at net.sf.hibernate.collection.AbstractCollectionPersister.writeKey(AbstractCollectionPersister.java:411) at net.sf.hibernate.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:487) at net.sf.hibernate.impl.ScheduledCollectionRemove.execute(ScheduledCollectionRemove.java:22) at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2449) at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2435) at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2394) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2261) at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61) at edu.academyart.model.TestA.testManyToManyReplicateWithNativeIds(TestA.java:42) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:31) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78) In this case, replicate calls doSave which calls OnReplicationVisitor.processValues in the case where the persistor has collections. It does this BEFORE it retrieves the generated id from the database. This causes a call to removeCollection() with a null id which schedules the removal with the null id and causes the eventual NullPointerException when trying to delete the collection. Below are my mappings and java objects to simplify the problem and the test case I used. The file A.hbm.xml has the sql to generate the needed tables. The test case won't compile for you because I use my hibernate session retrieval methods, but the concept is pretty straight forward. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping default-cascade="save-update"> <!-- CREATE TABLE [dbo].[A] ( [id] [bigint] IDENTITY (1, 1) NOT NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[B] ( [id] [bigint] IDENTITY (1, 1) NOT NULL ) ON [PRIMARY] GO ALTER TABLE [dbo].[A] WITH NOCHECK ADD PRIMARY KEY CLUSTERED ( [id] ) ON [PRIMARY] GO ALTER TABLE [dbo].[B] WITH NOCHECK ADD PRIMARY KEY CLUSTERED ( [id] ) ON [PRIMARY] GO CREATE TABLE [dbo].[A_to_B] ( [aId] [bigint] NULL , [bId] [bigint] NULL ) ON [PRIMARY] GO --> <class name="edu.academyart.model.A" dynamic-update="true" lazy="true" table="A"> <id name="id" type="long" column="id"> <generator class="native"/> </id> </class> </hibernate-mapping> <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping default-cascade="save-update"> <!-- edu.academyart.model.Course root --> <class name="edu.academyart.model.B" dynamic-update="true" lazy="true" table="B"> <!--<cache usage="nonstrict-read-write" />--> <id name="id" type="long" column="id"> <generator class="native"/> </id> <set name="setOfAs" table="A_to_B" lazy="true" cascade="all-delete-orphan" > <key column="bId"/> <many-to-many class="edu.academyart.model.A" column="aId"/> </set> </class> </hibernate-mapping> package edu.academyart.model; import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.validator.GenericValidator; import org.apache.log4j.Logger; import java.util.*; import java.util.regex.Pattern; import java.sql.ResultSet; import java.sql.PreparedStatement; import net.sf.hibernate.HibernateException; import net.sf.hibernate.Session; import net.sf.hibernate.Hibernate; import net.sf.hibernate.proxy.HibernateProxyHelper; import net.sf.hibernate.type.Type; public class A implements Serializable{ public final static Logger logger = Logger.getLogger(A.class.getName()); /** identifier field */ private Long id; /** default constructor */ public A() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String toString() { return new ToStringBuilder(this) .append("id", getId()) .toString(); } public boolean equals(Object other) { if ( !(other instanceof A) ) return false; A castOther = (A) other; return new EqualsBuilder() .append(this.getId(), castOther.getId()) .isEquals(); } public int hashCode() { return new HashCodeBuilder() .append(getId()) .toHashCode(); } } package edu.academyart.model; import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.validator.GenericValidator; import org.apache.log4j.Logger; import java.util.*; import java.util.regex.Pattern; import java.sql.ResultSet; import java.sql.PreparedStatement; import net.sf.hibernate.HibernateException; import net.sf.hibernate.Session; import net.sf.hibernate.Hibernate; import net.sf.hibernate.proxy.HibernateProxyHelper; import net.sf.hibernate.type.Type; public class B implements Serializable{ public final static Logger logger = Logger.getLogger(B.class.getName()); /** identifier field */ private Long id; private Set setOfAs; /** default constructor */ public B() { this.setOfAs= new HashSet(); } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Set getSetOfAs() { return setOfAs; } public void setSetOfAs(Set setOfAs) { this.setOfAs = setOfAs; } public String toString() { return new ToStringBuilder(this) .append("id", getId()) .toString(); } public boolean equals(Object other) { if ( !(other instanceof B) ) return false; B castOther = (B) other; return new EqualsBuilder() .append(this.getId(), castOther.getId()) .isEquals(); } public int hashCode() { return new HashCodeBuilder() .append(getId()) .toHashCode(); } } package edu.academyart.model; import edu.academyart.model.*; import edu.academyart.webapp.common.Constants; import edu.academyart.webapp.action.BaseHttpUnitTest; import edu.academyart.BaseUnitTest; import net.sf.hibernate.*; import com.roundpeg.webapp.common.ModelLoader; import java.util.*; import java.util.regex.Pattern; import java.util.regex.Matcher; import com.meterware.httpunit.*; import junit.framework.*; import org.apache.log4j.Logger; import org.apache.commons.net.ftp.FTPClient; public class TestA extends BaseUnitTest { public TestA ( String s ) { super( s ); } public void testManyToManyReplicateWithNativeIds() throws Exception { Session hibernate = getHibernateSession(); A a1 = new A(); A a2 = new A(); B b = new B(); b.getSetOfAs().add(a1); b.getSetOfAs().add(a2); hibernate.save(b); hibernate.flush(); hibernate.close(); Session archive = getHibernateSession("ARCHIVE"); logger.debug("BEGIN ARCHIVE"); Transaction tx = null; try{ tx = archive.beginTransaction(); archive.replicate(b, ReplicationMode.OVERWRITE); tx.commit(); } catch(Exception e) { tx.rollback(); throw e; } archive.close(); archive = getHibernateSession("ARCHIVE"); B archivedB = (B) archive.load(B.class, b.getId()); assertNotNull(archivedB); } }

    Hibernate JIRA | 1 decade ago | Marcus popetz
    java.lang.NullPointerException: null key for collection: edu.academyart.model.B.setOfAs

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.NullPointerException

      No message provided

      at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch()
    2. net.sf.hibernate
      JDBCTransaction.commit
      1. net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
      2. net.sf.hibernate.collection.CollectionPersister.recreate(CollectionPersister.java:705)
      3. net.sf.hibernate.impl.ScheduledCollectionRecreate.execute(ScheduledCollectionRecreate.java:22)
      4. net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2278)
      5. net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2238)
      6. net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2178)
      7. net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:56)
      7 frames
    3. com.access.sync
      ContactPersisterTester.testAll
      1. com.access.sync.framework.persistence.HibernatePersister.commit(HibernatePersister.java:311)
      2. com.access.sync.framework.persistence.test.ContactPersisterTester.myTestSave(ContactPersisterTester.java:126)
      3. com.access.sync.framework.persistence.test.ContactPersisterTester.testAll(ContactPersisterTester.java:79)
      3 frames
    4. Java RT
      Method.invoke
      1. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      2. sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      3. sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      4. java.lang.reflect.Method.invoke(Method.java:324)
      4 frames
    5. JUnit
      TestRunner.run
      1. junit.framework.TestCase.runTest(TestCase.java:154)
      2. junit.framework.TestCase.runBare(TestCase.java:127)
      3. junit.framework.TestResult$1.protect(TestResult.java:106)
      4. junit.framework.TestResult.runProtected(TestResult.java:124)
      5. junit.framework.TestResult.run(TestResult.java:109)
      6. junit.framework.TestCase.run(TestCase.java:118)
      7. junit.framework.TestSuite.runTest(TestSuite.java:208)
      8. junit.framework.TestSuite.run(TestSuite.java:203)
      9. junit.textui.TestRunner.doRun(TestRunner.java:116)
      10. junit.textui.TestRunner.doRun(TestRunner.java:109)
      11. junit.textui.TestRunner.run(TestRunner.java:72)
      12. junit.textui.TestRunner.run(TestRunner.java:57)
      12 frames