Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java 5 annotation does not work

Reply
Thread Tools

Java 5 annotation does not work

 
 
Marioli
Guest
Posts: n/a
 
      12-27-2006
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(us ername);
}

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(UserP ersistenceServiceBean.java:15)

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

Regards
Mariusz Lipiński

 
Reply With Quote
 
 
 
 
bg
Guest
Posts: n/a
 
      12-27-2006
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(us ername);
> }
>
> 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(UserP ersistenceServiceBean.java:15)
>
> The problem is that em variable (EntityManager) is null... but why? And
> how to fix it? Please help
>
> Regards
> Mariusz Lipiński
>

 
Reply With Quote
 
 
 
 
Daniel Dyer
Guest
Posts: n/a
 
      12-27-2006
On Wed, 27 Dec 2006 21:10:58 -0000, bg <(E-Mail Removed)> 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
 
Reply With Quote
 
Marioli
Guest
Posts: n/a
 
      12-28-2006
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 <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combining Java Reflection API with Java Annotation Types for Thread Safety pek Java 2 10-23-2007 03:00 PM
How does annotation injection work, while maintaining security? Danno Java 2 03-23-2007 04:19 PM
What is a Java Annotation? marcwentink@hotmail.com Java 10 05-31-2006 02:58 PM
java.text.Annotation srini Java 0 04-23-2004 11:25 PM
Getting Annotation for a Return Type (Java 1.5 Beta) Raymond McCrae Java 2 03-03-2004 06:37 PM



Advertisments