java.lang.NullPointerException

Hibernate JIRA | arjan tijms | 1 year ago
  1. 0

    Using an (aggregate) function in the select clause of a query causes a null pointer exception to be thrown. This happens when using either the JPA Criteria API or JPQL, and using either a constructor expression or a multi-valued select, and in case of the latter using either a {{Tuple}} or an {{Object[]}} return. The following shows example code with an hsqldb specific function: {code:title=JPQL query} SELECT new test.AggregatedTestEntity( FUNCTION('GROUP_CONCAT', _testEntity.value) ) FROM TestEntity _testEntity {code} {code:title=Entity} @Entity public class TestEntity { @Id @GeneratedValue(strategy = IDENTITY) public Long id; public String value; } {code} {code:title=Pojo used for ctor expr.} public class AggregatedTestEntity { private String values; public AggregatedTestEntity(String values) { this.values = values; } {code} Executing the above JPQL will cause the following exception to be thrown: {noformat} java.lang.NullPointerException at org.hibernate.internal.util.ReflectHelper.getConstructor(ReflectHelper.java:309) at org.hibernate.hql.internal.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:174) at org.hibernate.hql.internal.ast.tree.ConstructorNode.prepare(ConstructorNode.java:144) at org.hibernate.hql.internal.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:1092) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2328) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2194) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1476) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:573) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76) at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150) at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:298) at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236) at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1825) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:305) at org.jboss.as.jpa.container.AbstractEntityManager.createQuery(AbstractEntityManager.java:133) {noformat} A minimal test case is provided here: https://github.com/javaee-samples/javaee7-samples/tree/master/jpa/aggregate-function-in-select Easiest way to run just that test: * Edit [root pom.xml|https://github.com/javaee-samples/javaee7-samples] to contain only the JPA module: {code:xml} <modules> <module>test-utils</module> <module>jpa</module> <module>util</module> </modules> {code} * Start WildFly 10 * From the root of the project: mvn clean test _(note that WildFly is the default profile for the samples project, so {{-Pwildfly-remote-arquillian}} can be omitted)_ The problem seems to be that internally Hibernate keeps a list of {{Type}} instances for every element in the select clause which it matches against the constructor arguments. Only, for the {{FUNCTION}} this is {{Type}} is always {{null}} which eventually causes the NPE to be thrown. When an {{Object[]}} or {{Tuple}} is being returned the same problem occurs that the {{Type}} is always {{null}}. Even though there's no matching agains constructor arguments in that case, Hibernate still throws a NPE at some point.

    Hibernate JIRA | 1 year ago | arjan tijms
    java.lang.NullPointerException
  2. 0

    Using an (aggregate) function in the select clause of a query causes a null pointer exception to be thrown. This happens when using either the JPA Criteria API or JPQL, and using either a constructor expression or a multi-valued select, and in case of the latter using either a {{Tuple}} or an {{Object[]}} return. The following shows example code with an hsqldb specific function: {code:title=JPQL query} SELECT new test.AggregatedTestEntity( FUNCTION('GROUP_CONCAT', _testEntity.value) ) FROM TestEntity _testEntity {code} {code:title=Entity} @Entity public class TestEntity { @Id @GeneratedValue(strategy = IDENTITY) public Long id; public String value; } {code} {code:title=Pojo used for ctor expr.} public class AggregatedTestEntity { private String values; public AggregatedTestEntity(String values) { this.values = values; } {code} Executing the above JPQL will cause the following exception to be thrown: {noformat} java.lang.NullPointerException at org.hibernate.internal.util.ReflectHelper.getConstructor(ReflectHelper.java:309) at org.hibernate.hql.internal.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:174) at org.hibernate.hql.internal.ast.tree.ConstructorNode.prepare(ConstructorNode.java:144) at org.hibernate.hql.internal.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:1092) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2328) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2194) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1476) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:573) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76) at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150) at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:298) at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236) at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1825) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:305) at org.jboss.as.jpa.container.AbstractEntityManager.createQuery(AbstractEntityManager.java:133) {noformat} A minimal test case is provided here: https://github.com/javaee-samples/javaee7-samples/tree/master/jpa/aggregate-function-in-select Easiest way to run just that test: * Edit [root pom.xml|https://github.com/javaee-samples/javaee7-samples] to contain only the JPA module: {code:xml} <modules> <module>test-utils</module> <module>jpa</module> <module>util</module> </modules> {code} * Start WildFly 10 * From the root of the project: mvn clean test _(note that WildFly is the default profile for the samples project, so {{-Pwildfly-remote-arquillian}} can be omitted)_ The problem seems to be that internally Hibernate keeps a list of {{Type}} instances for every element in the select clause which it matches against the constructor arguments. Only, for the {{FUNCTION}} this is {{Type}} is always {{null}} which eventually causes the NPE to be thrown. When an {{Object[]}} or {{Tuple}} is being returned the same problem occurs that the {{Type}} is always {{null}}. Even though there's no matching agains constructor arguments in that case, Hibernate still throws a NPE at some point.

    Hibernate JIRA | 1 year ago | arjan tijms
    java.lang.NullPointerException
  3. 0

    Writing custom queries in Spring JPA

    Stack Overflow | 1 year ago | drunkenfist
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List EmployeeRepository.findByDeptId(java.lang.Long)!
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Exception in Hibernate Query- java.lang.IllegalArgumentException

    Stack Overflow | 10 months ago | arch
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ctxtGroupMasterDao': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.acinfotech.crowdtxt.jpa.dao.CtxtGroupMasterDao.findByCompanyIdAndReceiverConfigIdAndIsActive(java.lang.Integer,java.lang.Integer,java.lang.String)!

    4 unregistered visitors
    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 org.hibernate.internal.util.ReflectHelper.getConstructor()
    2. Hibernate
      SessionImpl.createQuery
      1. org.hibernate.internal.util.ReflectHelper.getConstructor(ReflectHelper.java:309)
      2. org.hibernate.hql.internal.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:174)
      3. org.hibernate.hql.internal.ast.tree.ConstructorNode.prepare(ConstructorNode.java:144)
      4. org.hibernate.hql.internal.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:1092)
      5. org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2328)
      6. org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2194)
      7. org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1476)
      8. org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:573)
      9. org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
      10. org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)
      11. org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262)
      12. org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)
      13. org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
      14. org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
      15. org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76)
      16. org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)
      17. org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:298)
      18. org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
      19. org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1825)
      19 frames
    3. org.hibernate.jpa
      AbstractEntityManagerImpl.createQuery
      1. org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:305)
      1 frame
    4. JPA
      AbstractEntityManager.createQuery
      1. org.jboss.as.jpa.container.AbstractEntityManager.createQuery(AbstractEntityManager.java:133)
      1 frame