0

in an application I m using JPA + hibernate. However when I m trying to access the db I m getting the following error :

 java.lang.NullPointerException: javax.faces.FacesException:
#{loginBean.authenticate}: java.lang.NullPointerException

the exception occurs when i execute the line: EntityManager em = emf.createEntityManager();

Below you see the full serverlog.

18:21:48,174 INFO  [org.hibernate.engine.transaction.internal.TransactionFactoryInitiator] (http--0.0.0.0-8080-1) HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory

18:21:48,177 INFO  [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (http--0.0.0.0-8080-1) HHH000397: Using ASTQueryTranslatorFactory

18:21:48,189 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000228: Running hbm2ddl schema update

18:21:48,191 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000102: Fetching database metadata

18:21:48,193 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000396: Updating schema

18:21:48,195 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000232: Schema update complete

18:22:08,153 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (http--0.0.0.0-8080-1) #{loginBean.authenticate}: java.lang.NullPointerException: javax.faces.FacesException: #{loginBean.authenticate}: java.lang.NullPointerException

    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) [jsf-impl-2.1.7-jbossorg-2.jar:]

    at javax.faces.component.UICommand.broadcast(UICommand.java:315) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]

    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]

    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]

    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) [jsf-impl-2.1.7-jbossorg-2.jar:]

    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.7-jbossorg-2.jar:]

    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl-2.1.7-jbossorg-2.jar:]

    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]

    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]

    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]

    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]

    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]

    at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]

    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]

    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]

    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]

    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]

    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]

    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]

    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]

    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]

    at java.lang.Thread.run(Unknown Source) [rt.jar:1.6.0_32]

Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException

    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]

    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) [jsf-impl-2.1.7-jbossorg-2.jar:]

    ... 21 more

Caused by: java.lang.NullPointerException

    at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:73) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:115) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1207) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:176) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.ejb.EntityManagerImpl.<init>(EntityManagerImpl.java:89) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:125) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:120) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]

    at org.Handlers.UserHandler.getAllUsers(UserHandler.java:32) [classes:]

    at org.tutorial.LoginBean.authenticate(LoginBean.java:50) [classes:]

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_32]

    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.6.0_32]

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.6.0_32]

    at java.lang.reflect.Method.invoke(Unknown Source) [rt.jar:1.6.0_32]

    at org.apache.el.parser.AstValue.invoke(AstValue.java:262) [jbossweb-7.0.13.Final.jar:]

    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) [jbossweb-7.0.13.Final.jar:]

    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) [jsf-impl-2.1.7-jbossorg-2.jar:]

    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]

    ... 22 more

My guess is that, for some reason , it has to do with the entity manager. I m really new to using hibernate and JPA . I also attach a class to show you how I access the db. Could someone figure out what I m doing wrong?

public class ProjectHandler {

    public void create(Project project) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ResearchManagement.Data");

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        em.persist(project);
        em.getTransaction().commit();
        em.close();
    }

    public List<Project> getAllProjects() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ResearchManagement.Data");

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();

        Query query = em.createQuery("SELECT p FROM Project p");
        List<Project> projects = query.getResultList();
        em.getTransaction().commit();
        em.close();
        return projects;
    }

    public int deleteByProjectName(String projectName) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ResearchManagement.Data");

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();

        Query q = em.createQuery("DELETE FROM Project p WHERE p.projectName = :u");
        q.setParameter("u", projectName );    
        int deleted = q.executeUpdate();
        em.getTransaction().commit();
        em.close();
        return deleted;
    }

}

Thnx for your answer and your tips about my code. However I debug my app and I see that the emf is not null . I still get the error at the line : EntityManager em = emf.createEntityManager();

So my guess is that the emf returns a null entityManager. Still doesnt understand why

What I noticed and looks strange the server log when I run the application. it reads the pesistence unit and connects to the db reading corerclty all the tables:

12:02:06,094 INFO [org.hibernate.validator.util.Version] (MSC service thread 1-3) Hibernate Validator 4.2.0.Final

12:02:06,328 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (MSC service thread 1-3) HHH000228: Running hbm2ddl schema update

12:02:06,328 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (MSC service thread 1-3) HHH000102: Fetching database metadata

12:02:06,328 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (MSC service thread 1-3) HHH000396: Updating schema

12:02:06,343 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000261: Table found: researchprojects.departments

12:02:06,343 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000037: Columns: [phonenumber, departmentid, name, city]

12:02:06,343 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000108: Foreign keys: []

12:02:06,343 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000126: Indexes: [primary]

12:02:06,359 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000261: Table found: researchprojects.employees

12:02:06,359 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000037: Columns: [phonenumber, departmentid, employeeid, lastname, firstname]

12:02:06,359 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000108: Foreign keys: [fk9d39ef85d77ba982]

12:02:06,359 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000126: Indexes: [fk9d39ef85d77ba982, primary]

12:02:06,374 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000261: Table found: researchprojects.partners

12:02:06,374 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000037: Columns: [partnerid, name, contactmail, city]

12:02:06,374 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000108: Foreign keys: []

12:02:06,374 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000126: Indexes: [primary]

12:02:06,374 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000261: Table found: researchprojects.projects

12:02:06,390 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000037: Columns: [departmentid, duration, description, projectname, projectid]

12:02:06,390 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000108: Foreign keys: [fkc479187ad77ba982]

12:02:06,390 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000126: Indexes: [primary, fkc479187ad77ba982]

12:02:06,390 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000261: Table found: researchprojects.projects_has_employees

12:02:06,390 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000037: Columns: [projects_projectid, employees_employeeid]

12:02:06,390 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000108: Foreign keys: [fk294f111bcd3ec025, fk294f111b92772ef4]

12:02:06,390 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000126: Indexes: [fk294f111bcd3ec025, primary, fk294f111b92772ef4]

12:02:06,406 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000261: Table found: researchprojects.projects_has_partners

12:02:06,406 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000037: Columns: [partners_partnerid, projects_projectid]

12:02:06,406 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000108: Foreign keys: [fk8d741055cd3ec025, fk8d7410555290eb4]

12:02:06,406 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000126: Indexes: [fk8d741055cd3ec025, fk8d7410555290eb4, primary]

12:02:06,421 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000261: Table found: researchprojects.users

12:02:06,421 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000037: Columns: [username, userid, password]

12:02:06,421 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000108: Foreign keys: []

12:02:06,421 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000126: Indexes: [primary]

12:02:06,421 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000261: Table found: researchprojects.hibernate_sequence

12:02:06,437 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000037: Columns: [next_val]

12:02:06,437 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000108: Foreign keys: []

12:02:06,437 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (MSC service thread 1-3) HHH000126: Indexes: []

12:02:06,437 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (MSC service thread 1-3) HHH000232: Schema update complete

However when I m trying to read the user table (getallUsers() ) , it creates the emf and at the log i see:

12:03:53,346 INFO [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (http--0.0.0.0-8080-1) HHH000397: Using ASTQueryTranslatorFactory

12:03:53,347 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000228: Running hbm2ddl schema update

12:03:53,347 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000102: Fetching database metadata

12:03:53,347 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000396: Updating schema

12:03:53,347 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000232: Schema update complete

That means that it cannot read the tables anymore. Does this mean anything to you ?

casperOne
  • 73,706
  • 19
  • 184
  • 253
panipsilos
  • 2,219
  • 11
  • 37
  • 53

2 Answers2

2

The error is caused by Hibernate internals, at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:73) This likely means that you have an issue with JTA configuration or classes. Since you are attempting to start a user transaction later on, you might have incorrectly marked it as JTA instead of resource_local.

This isn't a full answer as I'm not familiar with the code, but hope it helps point in the right direction.

Chris
  • 20,138
  • 2
  • 29
  • 43
  • Thnx . You were right. So the solution is to add the transaction-type="RESOURCE_LOCAL in the Peristence.xml file. – panipsilos Oct 24 '12 at 18:44
0

Based on the NullPointerException and the line you say it is thrown at being:

EntityManager em = emf.createEntityManager();

I am also assuming it is at:

org.tutorial.LoginBean.authenticate(LoginBean.java:50).

Your emf is more than likely null. Based on the code you supplied you are creating the EntityManagerFactory every time you need an EntityManager. You are not supposed to do that. Essentially that is an expensive operation, and you need to do it once during app start up and then reuse it throughout your app. You can see Best practice to get EntityManagerFactory to see how to resolve that.


While I have you here though, I would like to give you some suggestions on the code you supplied. It is not really doing everything it should be. You should at a minimum have a try-finally block to handle the transaction and roll things back if necessary. Take for instance your create method:
public void create(Project project) {
        EntityManagerFactory emf =Persistence.createEntityManagerFactory("ResearchManagement.Data");

        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            //start the transaction
            tx.begin();
            em.persist(project);
            //commit it the transaction
            tx.commit();
        } finally {
            //if the transaction is still active then
            //than something bad happened, so we need to
            //roll it back
            if (tx.isActive()) {
               tx.rollback();
            }
            //close out the entity manager instance, we are
            //done with it here
            em.close();
        }
    }

The last thing is not as big, it is more about formatting and well honestly it could have just been the way you copy pasted it into your question that caused it. But if you do have that much whites pace in your code, it makes things harder to grasp. Use white space to essentially make things easier to read. To much and it is a pain in the ass scrolling, to little and well everything is squished together and harder to process.


EDIT: You commented:

Thnx for your answer and your tips about my code. However I debug my app and I see that the emf is not null . I still get the error at the line : EntityManager em = emf.createEntityManager();

So my guess is that the emf returns a null entityManager. Still doesnt understand why

If emf.createEntityManager() just returned a null EntityManager than you would not have a stacktrace pointing to that particular line, but to the line where a method is called on the instance em itself (for example maybe em.getTransaction()). So either emf is null or something that should not be null and is used in emf.createEntityManager() is null. Only two choices.

Community
  • 1
  • 1
Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102