7 Replies Latest reply on Mar 13, 2008 3:27 PM by Keith Naas

    NullPointerException when trying to access database

    Tina Kleb Newbie

      Hi guys,


      I got stick when trying to ccess the MySQl Database.
      persistence.xml looks like this:
        



      <persistence-unit name="fahrzeug_console" transaction-type="JTA">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:/fahrzeug_consoleDatasource</jta-data-source>
            <properties>
               <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
               <property name="hibernate.hbm2ddl.auto" value="validate"/>
               <property name="hibernate.show_sql" value="true"/>
               <property name="hibernate.format_sql" value="true"/>
               <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
            </properties>
         </persistence-unit>   
      </persistence>




      And my code looks like this ( the problem occurs in queryVertraege() )



      package com.console.beans;
      
      import java.sql.Connection;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Statement;
      import java.util.ArrayList;
      import java.util.Collection;
      import java.util.List;
      
      import javax.ejb.Remove;
      import javax.ejb.Stateful;
      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import javax.persistence.PersistenceContextType;
      
      import org.hibernate.annotations.Entity;
      import org.hibernate.annotations.Table;
      import org.jboss.seam.annotations.Factory;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.datamodel.DataModel;
      import org.jboss.seam.annotations.security.Restrict;
      
      import org.jboss.seam.ScopeType;
      
      import com.console.doing.DBConnection;
      import org.jboss.seam.security.Identity;
      
      
      @Stateful
      @Name("vertraege")
      @Restrict("#{identity.loggedIn}")
      public class Vertraege {
           
      
           @DataModel
           private List<Vertrag> vertraege = new ArrayList<Vertrag>();
           
           private int size;
           
           @PersistenceContext
           private EntityManager em;          
      
           public int getSize()               {     return size;     }
      
           public void setSize(int size)     {     
                this.size = size;
                System.out.print("size gesetzt: "+size);
           }     
           
           public void queryVertraege()     {
                vertraege = em.createQuery("select Fahrgestellnummer from _Vertrag").getResultList();
           }
           
           @Remove
           public void destroy()     {}
      }


      This is the bean



      package com.console.beans;
      
      import java.io.Serializable;
      import java.util.ArrayList;
      import java.util.Date;
      import java.util.List;
      
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.Table;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.datamodel.DataModel;
      
      @Entity
      //@Table(name="_Vertrag")
      @Name("vertrag")
      public class Vertrag implements Serializable{
      
           private int Fahrgestellnummer;
           private int id;
           private Date realesVertragsende;
           private Date regulaeresVertragsende;
      
          @Column(name="Fahrgestellnummer")
           public int getFahrgestellnummer() {
                return Fahrgestellnummer;
           }
           public void setFahrgestellnummer(int fahrgestellnummer) {
                Fahrgestellnummer = fahrgestellnummer;
           }
           @Id @GeneratedValue
          @Column(name="ID_Vertrag")
           public int getId() {
                return id;
           }
           public void setId(int id) {
                this.id = id;
           }
          @Column(name="Reales_Vertragsende")
           public Date getRealesVertragsende() {
                return realesVertragsende;
           }
           public void setRealesVertragsende(Date realesVertragsende) {
                this.realesVertragsende = realesVertragsende;
           }
          @Column(name="Regulaeres_Vertragsende")
           public Date getRegulaeresVertragsende() {
                return regulaeresVertragsende;
           }
           public void setRegulaeresVertragsende(Date regulaeresVertragsende) {
                this.regulaeresVertragsende = regulaeresVertragsende;
           }
      
           @Override
           public String toString(){          
                return "Vertrag " +id+ ": Reales Vertragsende " +realesVertragsende+ ", reguläres Vertragsende " +regulaeresVertragsende;
           }
      }




      the following exception is thrown:



      12:24:40,812 FATAL [application] java.lang.NullPointerException
      javax.faces.el.EvaluationException: java.lang.NullPointerException
           at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:91)
           at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
           at javax.faces.component.UICommand.broadcast(UICommand.java:383)
           at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:184)
           at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:162)
           at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:350)
           at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
           at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
           at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
           at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
           at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141)
           at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
           at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:60)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:68)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
           at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
           at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
           at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
           at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
           at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
           at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
           at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
           at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
           at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
           at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
           at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
           at java.lang.Thread.run(Thread.java:619)
      Caused by: java.lang.NullPointerException
           at com.console.beans.Vertraege.queryVertraege(Vertraege.java:56)
           at com.console.beans.Login.login(Login.java:58)
           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:597)
           at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
           at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
           at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:46)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.bpm.BusinessProcessInterceptor.aroundInvoke(BusinessProcessInterceptor.java:49)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
           at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:166)
           at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:102)
           at com.console.beans.Login_$$_javassist_43.login(Login_$$_javassist_43.java)
           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:597)
           at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:329)
           at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:342)
           at org.jboss.el.parser.AstPropertySuffix.invoke(AstPropertySuffix.java:58)
           at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
           at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
           at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
           at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:77)
           ... 46 more




           
      Can somebody please tell me what is wrong?
      Do I have to do any configurations?


      Thanks Tina

        • 1. Re: NullPointerException when trying to access database
          Nicklas Karlsson Master

          Your entitymanager isn't injected correcly? Show your components.xml, there might be a naming (em/entityManager) mismatch...

          • 2. Re: NullPointerException when trying to access database
            Tina Kleb Newbie

            thanks for your help!


            This is my components.xml:




            <?xml version="1.0" encoding="UTF-8"?>
            <components xmlns="http://jboss.com/products/seam/components"
                        xmlns:core="http://jboss.com/products/seam/core"
                        xmlns:persistence="http://jboss.com/products/seam/persistence"
                        xmlns:drools="http://jboss.com/products/seam/drools"
                        xmlns:bpm="http://jboss.com/products/seam/bpm"
                        xmlns:security="http://jboss.com/products/seam/security"
                        xmlns:mail="http://jboss.com/products/seam/mail"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xsi:schemaLocation=
                            "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.0.xsd 
                             http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.0.xsd 
                             http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.0.xsd
                             http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.0.xsd
                             http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.0.xsd
                             http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.0.xsd
                             http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.0.xsd">
            
               <core:init debug="true" jndi-pattern="fahrzeug_console/#{ejbName}/local"/>
                 
                  
               <persistence:managed-persistence-context name="entityManager"
                                                 auto-create="true"
                                      entity-manager-factory="#{fahrzeug_consoleEntityManagerFactory}"/>
            
               <persistence:entity-manager-factory name="fahrzeug_consoleEntityManagerFactory" 
                                  persistence-unit-name="fahrzeug_console"/>
               
               <drools:rule-base name="securityRules">
                   <drools:rule-files><value>/security.drl</value></drools:rule-files>
               </drools:rule-base>
            
               <security:identity authenticate-method="#{authenticator.authenticate}"
                                       security-rules="#{securityRules}"
                                          remember-me="true"/>
                                          
               <event type="org.jboss.seam.security.notLoggedIn">
                   <action execute="#{redirect.captureCurrentView}"/>
               </event>
               <event type="org.jboss.seam.security.loginSuccessful">
                   <action execute="#{redirect.returnToCapturedView}"/>
               </event>
               
               <mail:mail-session host="localhost" port="2525" username="test" password="test" />
                    
               <!-- For use with jBPM pageflow or process management -->
                 
               <bpm:jbpm>
                  <bpm:process-definitions></bpm:process-definitions>
                  <!-- <bpm:pageflow-definitions></bpm:pageflow-definitions> -->
               </bpm:jbpm>
               
                  
            </components>
            



            I renamed the EntityManager in the code to entityManager but there is still a NullPointerException....

            • 3. Re: NullPointerException when trying to access database
              Christian Bauer Master

              Tina Kleb wrote on Mar 13, 2008 02:10 PM:


                <persistence:managed-persistence-context name="entityManager"
                                                   auto-create="true"
                                        entity-manager-factory="#{fahrzeug_consoleEntityManagerFactory}"/>
              
                 <persistence:entity-manager-factory name="fahrzeug_consoleEntityManagerFactory" 
                                    persistence-unit-name="fahrzeug_console"/>
                 
              





              This is not used at all if you apply @PersistenceContext injection through the EJB container. This configuration is for a Seam-managed and injected persistence context with @In. The docs are fairly extensive about both of these options.


              • 4. Re: NullPointerException when trying to access database
                Keith Naas Novice

                You're mixing a Seam Managed Persistence Context with a Container Managed PC.  @PersistenceContext will inject the entity manager based on what is in persistence.xml.  If persistence.xml isn't configured to access the seam entity manager, NPEs will likely occur.


                From 8.3. Seam-managed persistence contexts




                Of course, you need to make sure that you have bound the EntityManagerFactory into JNDI. In JBoss, you can do this by adding the following property setting to persistence.xml.

                <property name="jboss.entity.manager.factory.jndi.name" 
                          value="java:/EntityManagerFactories/bookingData"/>




                Now we can have our EntityManager injected using:


                @In EntityManager bookingDatabase;




                • 5. Re: NullPointerException when trying to access database
                  Christian Bauer Master

                  And from the stacktrace it looks to me like you are not even using an EJB container, so @Stateful and @PersistenceContext and all the rest have absolutely no effect...

                  • 6. Re: NullPointerException when trying to access database
                    Tina Kleb Newbie

                    first: thanks @all!


                    now i am totally confused!
                    i had a look at the seam userguide chapter 1.6 because i have to query the database (do not query the databse for processes or instances) and show the results. To use rowCount i thought i need @Persistence Context like


                    <h:dataTable id="hotels" value="#{hotels}" var="hot" rendered="#{hotels.rowCount>0}">



                    but i could not found any configuration in the persistence.xml or components.xml of the project...


                    so can you tell what's right and what's wrong?



                    • 7. Re: NullPointerException when trying to access database
                      Keith Naas Novice

                      It might be clearer after reading up on Seam managed PersistenceContexts.


                      Sounds like you want to use @In EntityManager entityManager instead of @PersistenceContext Entitymanager entityManager


                      Good luck,
                      keith