-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: @PersistenceContext, EntityManager and NullPointerException
PostPosted: Wed Apr 13, 2011 10:18 am 
Newbie

Joined: Wed Apr 13, 2011 10:03 am
Posts: 3
hi,
I know it's a problem widely discussed, but I read forums from two days and everything looks correct, please help me.
I have netbeans 7, tomcat 7, hibernate 3.6.3 and JavaServer Faces 2.

this is the structure of the project:
Code:
./src:
conf  java

./src/conf:
MANIFEST.MF  persistence.xml

./src/java:
AppBean.java  Bundle.properties  Bundles  Controller  Converter  DB  Utils

./src/java/Bundles:
Operatore.properties

./src/java/Controller:
OperatoreController.java

./src/java/Converter:
OperatoreConverter.java

./src/java/DB:
Operatore.java

./web:
index.xhtml  META-INF  operatore.xhtml  WEB-INF

./web/META-INF:
context.xml

./web/WEB-INF:
beans.xml  faces-config.xml  web.xml


and this OperatoreController.java:
Code:
package Controller;


import DB.Operatore;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@ManagedBean(name = "OperatoreController")
@SessionScoped
public class OperatoreController implements Serializable {

    @PersistenceContext(unitName = "mPU")
    private EntityManager entityManager;
    private Operatore x = new Operatore();

    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public Operatore getX() {
        return x;
    }

    public void setX(Operatore x) {
        this.x = x;
    }

    public void aggiungi() {
        if (x.getId() == 0) {
            x.setId(null);
        }
        entityManager.persist(x);
    }

    public void modifica(Object id) {
        x = entityManager.find(Operatore.class, id);
    }

    public void elimina(Object id) {
        entityManager.remove(entityManager.find(Operatore.class, id));
    }

    public List<Operatore> list() {
        System.err.println("\n\n" + entityManager + "\n\n");
        List<Operatore> list = entityManager.createQuery("FROM Operatore").getResultList();
        return list;
    }

    public Operatore get(Integer id) {
        Operatore list = (Operatore) entityManager.createQuery("FROM Operatore AS x WHERE x.id = :id").setParameter("id", id).getSingleResult();
        return list;
    }
}


from this file I use only "list" function.

this is my persistance.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<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="mPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>DB.Operatore</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <shared-cache-mode>NONE</shared-cache-mode>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/MY_DB"/>
      <property name="javax.persistence.jdbc.password" value="MY_PWD"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.user" value="MY_USER"/>
    </properties>
  </persistence-unit>
</persistence>


and finally this is the stack trace:
Code:
java.lang.NullPointerException
   at Controller.OperatoreController.list(OperatoreController.java:117)
   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 javax.el.BeanELResolver.invoke(BeanELResolver.java:467)
   at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:161)
   at org.apache.el.parser.AstValue.getValue(AstValue.java:159)
   at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
   at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
   at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:193)
   at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:181)
   at javax.faces.component.UIData.getValue(UIData.java:557)
   at javax.faces.component.UIData.getDataModel(UIData.java:1297)
   at javax.faces.component.UIData.setRowIndex(UIData.java:450)
   at com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:81)
   at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:828)
   at javax.faces.component.UIData.encodeBegin(UIData.java:940)
   at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:55)
   at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:46)
   at org.primefaces.component.panel.PanelRenderer.encodeContent(PanelRenderer.java:225)
   at org.primefaces.component.panel.PanelRenderer.encodeMarkup(PanelRenderer.java:148)
   at org.primefaces.component.panel.PanelRenderer.encodeEnd(PanelRenderer.java:75)
   at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:883)
   at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1659)
   at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
   at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:853)
   at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1652)
   at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1655)
   at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1655)
   at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:399)
   at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
   at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
   at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
   at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
   at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
   at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
   at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
   at java.lang.Thread.run(Thread.java:662)


Thanks to all


Top
 Profile  
 
 Post subject: Re: @PersistenceContext, EntityManager and NullPointerException
PostPosted: Wed Apr 13, 2011 11:10 am 
Newbie

Joined: Tue Sep 14, 2010 4:29 pm
Posts: 16
Hi syco,

the persistence.xml has to be present in the META-INF folder. Check the the resulting war/jar or the exploded webapp folders, if its there. I am not familiar with project structure under Netbeans, but I think moving the persistence.xml from ./src/conf to ./web/META-INF might do the trick.

Greetings ngomo

_________________
http://www.winfonet.eu


Top
 Profile  
 
 Post subject: Re: @PersistenceContext, EntityManager and NullPointerException
PostPosted: Wed Apr 13, 2011 12:32 pm 
Newbie

Joined: Wed Apr 13, 2011 10:03 am
Posts: 3
i unpacked the final war, this is the folder tree:
Code:
ls -R
.:
index.xhtml  META-INF  operatore.xhtml  WEB-INF

./META-INF:
context.xml  MANIFEST.MF

./WEB-INF:
beans.xml  classes  faces-config.xml  lib  web.xml

./WEB-INF/classes:
AppBean.class  BUNDLE  Bundle.properties  Controller  Converter  DB  META-INF  Utils

./WEB-INF/classes/BUNDLE:
Operatore.properties

./WEB-INF/classes/Controller:
OperatoreController.class

./WEB-INF/classes/Converter:
OperatoreConverter.class

./WEB-INF/classes/DB:
Operatore.class

./WEB-INF/classes/META-INF:
persistence.xml

./WEB-INF/classes/Utils:
Functions.class

./WEB-INF/lib:
antlr-2.7.6.jar              hibernate-jpa-2.0-api-1.0.0.Final.jar  jsf-impl.jar                         primefaces.jar
commons-collections-3.1.jar  hibernate-testing.jar                  jstl.jar                             slf4j-api-1.6.1.jar
dom4j-1.6.1.jar              javassist-3.12.0.GA.jar                jta-1.1.jar                          standard.jar
hibernate3.jar               jsf-api.jar                            mysql-connector-java-5.1.13-bin.jar

persistance.xml is under a META-INF folder, i have two of them.

i tried to move the file from src/conf to web/META-INF, this is the unpacked result:
Code:
.:
index.xhtml  META-INF  operatore.xhtml  WEB-INF

./META-INF:
context.xml  MANIFEST.MF  persistence.xml

./WEB-INF:
beans.xml  classes  faces-config.xml  lib  web.xml

./WEB-INF/classes:
AppBean.class  Bundle.properties  Bundles  Controller  Converter  DB  Utils

./WEB-INF/classes/Bundles:
Operatore.properties

./WEB-INF/classes/Controller:
OperatoreController.class

./WEB-INF/classes/Converter:
OperatoreConverter.class

./WEB-INF/classes/DB:
Operatore.class

./WEB-INF/classes/Utils:
Functions.class

./WEB-INF/lib:
antlr-2.7.6.jar              hibernate3.jar                         jsf-api.jar   mysql-connector-java-5.1.13-bin.jar
commons-collections-3.1.jar  hibernate-jpa-2.0-api-1.0.0.Final.jar  jsf-impl.jar  primefaces.jar
cupertino-1.0.1.jar          hibernate-testing.jar                  jstl.jar      slf4j-api-1.6.1.jar
dom4j-1.6.1.jar              javassist-3.12.0.GA.jar                jta-1.1.jar   standard.jar

but the error is the same.


Top
 Profile  
 
 Post subject: Re: @PersistenceContext, EntityManager and NullPointerException
PostPosted: Wed Apr 13, 2011 2:18 pm 
Newbie

Joined: Tue Sep 14, 2010 4:29 pm
Posts: 16
I have surfed the web a bit for documentation. I found the chapter Persistence Units in Sun's/Oracle's Java EE5 tutorial located here:http://download.oracle.com/javaee/5/tutorial/doc/bnbqw.html#bnbrj. They write:
Quote:
If you package the persistence unit as a set of classes in a WAR file, persistence.xml should be located in the WAR file’s WEB-INF/classes/META-INF directory.
Your persistence.xml being in WEB-INF/classes/META-INF/ and your entity class being in WEB-INF/classes/DB/ should work.

I thought that dependency injeciton of JSF might be the issue. I have read a bit further up to the chapter located here: http://download.oracle.com/javaee/5/tutorial/doc/bnbrm.html#bnbrp
I found the following paragraph that might be of importance for you:
Quote:
In fact, you can bypass injecting EntityManagerFactory and instead inject the EntityManager directly into BookDBAO. This is because thread safety is not an issue with request-scoped beans. Conversely, developers need to be concerned with thread safety when working with servlets and listeners. Therefore, a servlet or listener needs to inject an EntityManagerFactory instance, which is thread-safe, whereas a persistence context is not thread-safe. The following code shows part of the BookDBAO object included in the JavaServer Faces version of Duke’s Bookstore:
You marked your Managed Bean as SessionScoped which (opposed to RequestScoped) is not thread safe. So maybe this prevents EntityManager from being injected. I suggest trying one of the following. Either mark your Managed Bean RequestScoped or inject the EntityManagerFactory with @PersistenceUnit and get the EntityManager from the factory.

If this does not help, I am sorry to admit that I lack any further ideas.

Cheers ngomo

_________________
http://www.winfonet.eu


Top
 Profile  
 
 Post subject: Re: @PersistenceContext, EntityManager and NullPointerException
PostPosted: Wed Apr 13, 2011 3:42 pm 
Newbie

Joined: Wed Apr 13, 2011 10:03 am
Posts: 3
IT WORKS!
thanks a lot.

and for who has an annoying
Code:
org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags

should substitute java.utils.List with java.utils.Set in Entity declaretion.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.