10 Replies Latest reply on Oct 24, 2012 5:42 AM by panipsilos

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

    panipsilos

      Hi guys , 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 shwo 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;

       

          }

        • 1. Re: java.lang.NullPointerException: javax.faces.FacesException: #{loginBean.authenticate}: java.lang.NullPointerException
          nickarls

          What happens in at org.Handlers.UserHandler.getAllUsers(UserHandler.java:32) ?

          • 2. Re: java.lang.NullPointerException: javax.faces.FacesException: #{loginBean.authenticate}: java.lang.NullPointerException
            panipsilos

            public class UserHandler {

               

                public void create(User user)

                {

                    EntityManagerFactory emf = Persistence.createEntityManagerFactory("ResearchManagement.Data");

                    EntityManager em = emf.createEntityManager();

                    em.getTransaction().begin();

                    em.persist(user);

                   

                    em.getTransaction().commit();

                    em.close();

                   

                }

               

                public List<User> getAllUsers()

               

                {

                    EntityManagerFactory emf = Persistence.createEntityManagerFactory("ResearchManagement.Data");

            Line 32 --->       EntityManager em = emf.createEntityManager();

                    em.getTransaction().begin();

                   

                    Query query = em.createQuery("SELECT p FROM User p");

                    List<User> users = query.getResultList();

                   

                    em.getTransaction().commit();

                    em.close();

                   

                    return users;

                }

               

                public int deleteByUsername(String userName)

                {

                    EntityManagerFactory emf = Persistence.createEntityManagerFactory("ResearchManagement.Data");

                    EntityManager em = emf.createEntityManager();

                    em.getTransaction().begin();

                   

                    Query q = em.createQuery("DELETE FROM User u WHERE u.userName = :u");

                    q.setParameter("u", userName );

                    int deleted = q.executeUpdate();

                   

                    em.getTransaction().commit();

                    em.close();

                   

                    return deleted;

             

                }

             

            }

             

            At line 32 I create the entity manager. So again my guess that the entity manager is null, but I cant get why.

            • 3. Re: java.lang.NullPointerException: javax.faces.FacesException: #{loginBean.authenticate}: java.lang.NullPointerException
              nickarls

              It could also be that emf is null (is the name of the PU correct)?

               

              More to the point, what is the running environment? Why aren't you using @PersistenceContext injection for your EntityManager?

              • 4. Re: java.lang.NullPointerException: javax.faces.FacesException: #{loginBean.authenticate}: java.lang.NullPointerException
                nickarls

                something like

                 

                @Stateful
                @Model
                public class UserBean {
                 @PersistenceContext
                  private EntityManager em;
                
                  public void save() {
                    em.persist(something);
                  }
                
                }
                
                

                 

                would appear to do the job without the pesky stuff of managing transactions and getting entitymanagers....

                • 5. Re: java.lang.NullPointerException: javax.faces.FacesException: #{loginBean.authenticate}: java.lang.NullPointerException
                  panipsilos

                  Well I m using JPA and Hibernate for data layer and JSF for UI. i m running it on a JBoss. I auto-generated all the entities using the JPA project from Eclipse. So i didnt really concern with using annotations.

                  Thing is that I m pretty new to all these frameowrks and I m missing basic knowledge.  Could you please recommend me a good tutorial/handbook for getting an insight on these things?

                   

                  Concerning the PU, the name is correct since I had it workign before switiching to using a datasource.

                  Initally I had the configuration of the connection to the database in the persistence.xml , without using any -ds.xml or the standalone.xml

                   

                  <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

                      <persistence-unit name="ResearchManagement.Data">

                          <class>org.Entities.Department</class>

                          <class>org.Entities.Employee</class>

                          <class>org.Entities.Partner</class>

                          <class>org.Entities.Project</class>

                          <class>org.Entities.User</class>

                   

                      <properties>

                      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

                      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>     

                      <property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/researchprojects"/>

                      <property name="hibernate.connection.username" value="root"/>

                      <property name="hibernate.connection.password" value="root"/>

                      <property name="hibernate.hbm2ddl.auto" value="update"/>

                   

                      </properties>

                      </persistence-unit>

                  </persistence>

                  • 6. Re: java.lang.NullPointerException: javax.faces.FacesException: #{loginBean.authenticate}: java.lang.NullPointerException
                    nickarls

                    (Besides, creating an EMF for each operation is very expensive)

                     

                    I think you'll find various tutorials from http://www.jboss.org/developer

                    • 7. Re: java.lang.NullPointerException: javax.faces.FacesException: #{loginBean.authenticate}: java.lang.NullPointerException
                      panipsilos

                      thnx , once again you were very helpful to me.

                      • 8. Re: java.lang.NullPointerException: javax.faces.FacesException: #{loginBean.authenticate}: java.lang.NullPointerException
                        panipsilos

                        Well actually the problem still remains. I debug the application and I see that the emf is not null. However it seems to return a null entity manager.

                        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 ?

                        • 9. Re: java.lang.NullPointerException: javax.faces.FacesException: #{loginBean.authenticate}: java.lang.NullPointerException
                          nickarls

                          A non-null EMF returning a null EM without an exception being thrown sounds a bit strange.

                           

                          I'm not sure what side-effects running multiple EMF boostrapping calls with name="hibernate.hbm2ddl.auto" value="update"/> could do but apparently the tables themselves survive.

                          • 10. Re: java.lang.NullPointerException: javax.faces.FacesException: #{loginBean.authenticate}: java.lang.NullPointerException
                            panipsilos

                            Ok I ll investigate further into it. Thnk you for your help