Java 5 annotation does not work

Discussion in 'Java' started by Marioli, Dec 27, 2006.

  1. Marioli

    Marioli Guest

    Hi all,

    I have modified slightly (to learn from it:) NetBeans 5.5 JsfJpa
    example application and thus coused it to stop working. I get
    NullPointerException. I have made following modifications:

    I have modified getUser() call in UserManager class to delegate the job
    to the different class:

    private Wuser getUser() {
    /* try {
    Wuser user = (Wuser)
    em.createNamedQuery("Wuser.findByUsername").
    setParameter("username",
    username).getSingleResult();
    return user;
    } catch (NoResultException nre) {
    return null;
    }
    */
    return (new
    dao.UserPersistenceServiceBean()).getUserByName(username);
    }

    and added new class:

    package dao;

    import enterprise.jsf_jpa_war.Wuser;

    import javax.persistence.PersistenceContext;
    import javax.persistence.EntityManager;
    import javax.persistence.NoResultException;

    public class UserPersistenceServiceBean {
    @PersistenceContext
    private EntityManager em;

    public Wuser getUserByName( String userName ) {
    try {
    return
    (Wuser)em.createNamedQuery("Wuser.findByUsername").setParameter("username",
    userName).getSingleResult();
    } catch( NoResultException nrExc ) {
    return null;
    }
    }
    }

    the result is:

    java.lang.NullPointerException
    dao.UserPersistenceServiceBean.getUserByName(UserPersistenceServiceBean.java:15)

    The problem is that em variable (EntityManager) is null... but why? And
    how to fix it? Please help:)

    Regards
    Mariusz Lipiñski
     
    Marioli, Dec 27, 2006
    #1
    1. Advertising

  2. Marioli

    bg Guest

    Of course em is null, you haven't instantiated it!

    You need something like:
    private EntityManager em = new EntityManager(...);

    Marioli wrote:
    > Hi all,
    >
    > I have modified slightly (to learn from it:) NetBeans 5.5 JsfJpa
    > example application and thus coused it to stop working. I get
    > NullPointerException. I have made following modifications:
    >
    > I have modified getUser() call in UserManager class to delegate the job
    > to the different class:
    >
    > private Wuser getUser() {
    > /* try {
    > Wuser user = (Wuser)
    > em.createNamedQuery("Wuser.findByUsername").
    > setParameter("username",
    > username).getSingleResult();
    > return user;
    > } catch (NoResultException nre) {
    > return null;
    > }
    > */
    > return (new
    > dao.UserPersistenceServiceBean()).getUserByName(username);
    > }
    >
    > and added new class:
    >
    > package dao;
    >
    > import enterprise.jsf_jpa_war.Wuser;
    >
    > import javax.persistence.PersistenceContext;
    > import javax.persistence.EntityManager;
    > import javax.persistence.NoResultException;
    >
    > public class UserPersistenceServiceBean {
    > @PersistenceContext
    > private EntityManager em;
    >
    > public Wuser getUserByName( String userName ) {
    > try {
    > return
    > (Wuser)em.createNamedQuery("Wuser.findByUsername").setParameter("username",
    > userName).getSingleResult();
    > } catch( NoResultException nrExc ) {
    > return null;
    > }
    > }
    > }
    >
    > the result is:
    >
    > java.lang.NullPointerException
    > dao.UserPersistenceServiceBean.getUserByName(UserPersistenceServiceBean.java:15)
    >
    > The problem is that em variable (EntityManager) is null... but why? And
    > how to fix it? Please help:)
    >
    > Regards
    > Mariusz Lipiñski
    >
     
    bg, Dec 27, 2006
    #2
    1. Advertising

  3. Marioli

    Daniel Dyer Guest

    On Wed, 27 Dec 2006 21:10:58 -0000, bg <> wrote:

    > Of course em is null, you haven't instantiated it!
    >
    > You need something like:
    > private EntityManager em = new EntityManager(...);
    >


    Well that is the problem, but the expectation for it to be non-null is not
    unreasonable. The OP is using the PersistenceContext annotation to
    express a dependency on the EntityManager. The idea is that the
    application server should inject the dependency so that it is not
    necessary to instantiate the EntityManager. Presumably the OP is not
    running this example inside an application server, or the application
    server is mis-configured.

    Dan.


    --
    Daniel Dyer
    https://watchmaker.dev.java.net - Evolutionary Algorithm Framework for Java
     
    Daniel Dyer, Dec 27, 2006
    #3
  4. Marioli

    Marioli Guest

    Yes, application server is suposed to set this dependency...
    application is running on Sun AS 9 and JDK 1.5. Before I've made this
    simple modification it was all ok - dependencies were set by AS so I
    guess AS is configured just OK. Well... Java 5 looks great at first
    sight and then... strange difficulties. Why I don't have an exception
    that the dependency cannot be set becouse of...

    Regards
    Mariusz Lipiñski

    On 27 Gru, 22:41, "Daniel Dyer" <"You don't need it"> wrote:
    > On Wed, 27 Dec 2006 21:10:58 -0000, bg <> wrote:
    > > Of course em is null, you haven't instantiated it!

    >
    > > You need something like:
    > > private EntityManager em = new EntityManager(...);Well that is the problem, but the expectation for it to be non-null is not

    > unreasonable. The OP is using the PersistenceContext annotation to
    > express a dependency on the EntityManager. The idea is that the
    > application server should inject the dependency so that it is not
    > necessary to instantiate the EntityManager. Presumably the OP is not
    > running this example inside an application server, or the application
    > server is mis-configured.
    >
    > Dan.
    >
    > --
    > Daniel Dyerhttps://watchmaker.dev.java.net- Evolutionary Algorithm Framework for Java
     
    Marioli, Dec 28, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond McCrae
    Replies:
    2
    Views:
    1,607
    Raymond McCrae
    Mar 3, 2004
  2. srini

    java.text.Annotation

    srini, Apr 24, 2004, in forum: Java
    Replies:
    0
    Views:
    554
    srini
    Apr 24, 2004
  3. Replies:
    10
    Views:
    844
    Chris Uppal
    May 31, 2006
  4. Danno
    Replies:
    2
    Views:
    694
    Danno
    Mar 23, 2007
  5. pek
    Replies:
    2
    Views:
    1,122
Loading...

Share This Page