Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

nullPointerException on entity manager methods

 
Martin Frisone
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 959
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Can you provide us with the code and the stacktrace?
     
    Martin Frisone
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    sure....

    Persistence Unit "minuto1PU"


    Entity class "Cliente"


    Main class "Minuto1"


     
    Martin Frisone
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    and forgot the stacktrace

     
    Freddy Wong
    Ranch Hand
    Posts: 959
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic