This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Object Relational Mapping and the fly likes nullPointerException on entity manager methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "nullPointerException on entity manager methods" Watch "nullPointerException on entity manager methods" New topic
Author

nullPointerException on entity manager methods

Martin Frisone
Greenhorn

Joined: Nov 05, 2007
Posts: 7
Hi,
I'm working on a desktop application that reads data from a database using persistence manager Toplink and the netbeans IDE.
I have set:

  • The persistence unit (persistence.xml)
  • @PersistenceContext annotation
  • "EntityManager em" class
  • the entity clases from the DB


  • the problem comes when I try to execute any of the entity manager methods, such as persist() or find(). It always throws a NullPointerException so I'd like to know where could be my problem. Any idea will help.
    Thanks in advance
    Freddy Wong
    Ranch Hand

    Joined: Sep 11, 2006
    Posts: 959

    Can you provide us with the code and the stacktrace?


    SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
    My Blog
    Martin Frisone
    Greenhorn

    Joined: Nov 05, 2007
    Posts: 7
    sure....

    Persistence Unit "minuto1PU"


    Entity class "Cliente"


    Main class "Minuto1"


    Martin Frisone
    Greenhorn

    Joined: Nov 05, 2007
    Posts: 7
    and forgot the stacktrace

    Freddy Wong
    Ranch Hand

    Joined: Sep 11, 2006
    Posts: 959

    Do you run this application in an EJB container? @PersistenceContext can only be used if you use EJB. If you use pure JPA, you need to create the EntityManager by yourself, e.g.

    Martin Frisone
    Greenhorn

    Joined: Nov 05, 2007
    Posts: 7
    Oh! yes, I see my mistake now... I'm not using an EJB containter just a desktop application, that's why the Container Managed Persistence won't work.

    I've tried the Application Managed Persistence and it worked. Problem Solved, thanks a lot
     
    wood burning stoves
     
    subject: nullPointerException on entity manager methods