1
public Guest getGuestByEmailAddress(String emailAddress) {
    try {
        String query 
            = "FROM Guest WHERE Guest.emailAddress = :emailAddress";
        if(entityManager == null) {
            System.out.println("null entityManager");
        } else {
            System.out.println("entity manager not null");
            System.out.println(query);
        }
        Query q = entityManager.createQuery(query);
        q.setParameter("emailAddress", emailAddress);
        return (Guest) q.getSingleResult();
    } catch (EntityNotFoundException e) {
        return null;
    }
}

When run, it prints entity manager not null. An earlier call to another method in the same class using the same entity manager is successful as well.

I have the stack trace:

Nov 04, 2017 3:50:44 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at java.lang.String$CaseInsensitiveComparator.compare(String.java:1192)
    at java.lang.String$CaseInsensitiveComparator.compare(String.java:1186)
    at java.util.TreeMap.getEntryUsingComparator(TreeMap.java:376)
    at java.util.TreeMap.getEntry(TreeMap.java:345)
    at java.util.TreeMap.get(TreeMap.java:278)
    at org.hibernate.dialect.function.SQLFunctionRegistry.findSQLFunction(SQLFunctionRegistry.java:45)
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.findSQLFunction(SessionFactoryHelper.java:369)
    at org.hibernate.hql.internal.ast.tree.IdentNode.getDataType(IdentNode.java:374)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.lookupProperty(HqlSqlWalker.java:654)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.addrExpr(HqlSqlBaseWalker.java:5003)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1286)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4707)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4175)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2138)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:815)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:609)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:266)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
    at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:553)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:662)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)
    at com.gitrekt.resort.model.services.GuestService.getGuestByEmailAddress(GuestService.java:49)
    at com.gitrekt.resort.GitRekt.testHibernate(GitRekt.java:95)
    at com.gitrekt.resort.GitRekt.start(GitRekt.java:46)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application com.gitrekt.resort.GitRekt

I should note that this is a Java SE environment. No dependency injection, EJB, Spring, or any of that fanciness. Why am I getting a null pointer exception when the entity manager is not null?

This is how I am managing the entityManager in the class where the method in question is called:

private final EntityManager entityManager;

    public GuestService() {
        this.entityManager = HibernateUtil.getEntityManager();
    }

    @Override
    public void finalize() throws Throwable {
        super.finalize();
        this.entityManager.close();
    }

I have called this method immediately before calling the method which throws the exception, and it runs properly:

public Guest getGuestById(Long id) {
        try {
            Guest guest = entityManager.getReference(Guest.class,id);
            return  guest;
        } catch (EntityNotFoundException e) {
            System.out.println("entity not found");
            return null;
        }
    }

What gives?

8
  • Entity manager is not null as seen from the stacktrace itself. WHy do you think EM is null? Commented Nov 4, 2017 at 20:18
  • Then what IS null? I'm clearly getting a null pointer exception. I tried running it through the debugger, and couldn't figure out why it is throwing the NPE Commented Nov 4, 2017 at 20:20
  • query is not null, and entityManager is not null, so what on earth is null? Commented Nov 4, 2017 at 20:20
  • If you see the stack trace closely, something else is null. To do that can you please include the line no. as well for your method in question. Commented Nov 4, 2017 at 20:22
  • 1
    Let us continue this discussion in chat. Commented Nov 4, 2017 at 20:29

1 Answer 1

4

The null pointer exception originates from framework code, not your code.

It seems your specific query causes the problem. Try changing it as follows:

SELECT g FROM Guest g WHERE g.emailAddress = :emailAddress

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.