Home > Archive > Netbeans J2EE > June 2006 > NPE from struts action with PersistenceUnit
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
NPE from struts action with PersistenceUnit
|
|
| Adam Jenkins 2006-06-05, 4:24 am |
| Hi Guys,
I've been tearing my hair out all day with Netbeans 5.5 and Glassfish. I'm writing a struts application, and I have a basic action, that is using the PersistenceUnit annotation to inject the EntityManagerFactory. Now I've tried this with a simple servle
t in the same project and it worked fine, but in my struts action (or in any java classes that aren't servlets for that matter) the EntityManagerFactory is null, and hence I get a NullPointerException.
The action gets called ok. Any assistence would really be appreciated.
The action code is:
public class EditProjectAction extends Action {
@PersistenceUnit
private EntityManagerFactory emf;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Project project = (Project)form;
emf.createEntityManager().persist(project);
return null;
}
}
The NPE is thrown at emf.createEntityManager() (emf is null).
Cheers
Adam
---------------------------------
On Yahoo!7
Socceroos Central: Latest news, schedule, blogs and videos.
| |
| Erno Mononen 2006-06-05, 4:24 am |
|
Hi,
You'll need to use lookup for getting the EMF in Struts actions, since
resource injection can not be used with classes that are not managed by
the container (it works for servlets since they're managed).
Erno
Adam Jenkins wrote:
> Hi Guys,
>
> I've been tearing my hair out all day with Netbeans 5.5 and
> Glassfish. I'm writing a struts application, and I have a basic
> action, that is using the PersistenceUnit annotation to inject the
> EntityManagerFactory. Now I've tried this with a simple servlet in
> the same project and it worked fine, but in my struts action (or in
> any java classes that aren't servlets for that matter) the
> EntityManagerFactory is null, and hence I get a NullPointerException.
>
> The action gets called ok. Any assistence would really be appreciated.
>
> The action code is:
>
> public class EditProjectAction extends Action {
>
> @PersistenceUnit
> private EntityManagerFactory emf;
>
> public ActionForward execute(ActionMapping mapping, ActionForm form,
> HttpServletRequest request, HttpServletResponse response)
> throws Exception {
> Project project = (Project)form;
> emf.createEntityManager().persist(project);
> return null;
> }
> }
>
> The NPE is thrown at emf.createEntityManager() (emf is null).
>
> Cheers
> Adam
>
> ------------------------------------------------------------------------
> On Yahoo!7
> Socceroos Central: Latest news, schedule, blogs and videos.
> <http://us.rd.yahoo.com/mail/tag/**h...m%2Ffootball%2F>
| |
| Craig McClanahan 2006-06-05, 8:14 am |
| On 6/5/06, Adam Jenkins <adamjenkinstmpredirect-/E1597aS9LT0CCvOHzKKcA@public.gmane.org> wrote:
>
> Hi Guys,
>
> I've been tearing my hair out all day with Netbeans 5.5 and Glassfish.
> I'm writing a struts application, and I have a basic action, that is using
> the PersistenceUnit annotation to inject the EntityManagerFactory. Now I've
> tried this with a simple servlet in the same project and it worked fine, but
> in my struts action (or in any java classes that aren't servlets for that
> matter) the EntityManagerFactory is null, and hence I get a
> NullPointerException.
>
The app server can only perform resource injection on object instances that
*it* creates, such as a servlet or filter ... not an instance created by an
application framework (such as Struts in this case). You might want to take
a look at using JSF, though, because JSF managed beans fall into the
category of object instances created by the container, so they do get
injection.
Craig
The action gets called ok. Any assistence would really be appreciated.
>
> The action code is:
>
> public class EditProjectAction extends Action {
>
> @PersistenceUnit
> private EntityManagerFactory emf;
>
> public ActionForward execute(ActionMapping mapping, ActionForm form,
> HttpServletRequest request, HttpServletResponse response)
> throws Exception {
> Project project = (Project)form;
> emf.createEntityManager().persist(project);
> return null;
> }
> }
>
> The NPE is thrown at emf.createEntityManager() (emf is null).
>
> Cheers
> Adam
>
> ------------------------------
> On Yahoo!7
> Socceroos Central: Latest news, schedule, blogs and videos.
> <http://us.rd.yahoo.com/mail/tag/**h...m%2Ffootball%2F>
>
>
| |
| hanasaki 2006-06-05, 8:14 am |
| What would be the preferred code for getting an emf instance in a
non-container instantiated (non-injectable) object instance?
Craig McClanahan wrote:
> On 6/5/06, *Adam Jenkins* <adamjenkinstmpredirect-/E1597aS9LT0CCvOHzKKcA@public.gmane.org
> <mailto:adamjenkinstmpredirect-/E1597aS9LT0CCvOHzKKcA@public.gmane.org>> wrote:
>
> Hi Guys,
>
> I've been tearing my hair out all day with Netbeans 5.5 and
> Glassfish. I'm writing a struts application, and I have a basic
> action, that is using the PersistenceUnit annotation to inject the
> EntityManagerFactory. Now I've tried this with a simple servlet in
> the same project and it worked fine, but in my struts action (or in
> any java classes that aren't servlets for that matter) the
> EntityManagerFactory is null, and hence I get a NullPointerException.
>
>
> The app server can only perform resource injection on object instances
> that *it* creates, such as a servlet or filter ... not an instance
> created by an application framework (such as Struts in this case). You
> might want to take a look at using JSF, though, because JSF managed
> beans fall into the category of object instances created by the
> container, so they do get injection.
>
> Craig
>
> The action gets called ok. Any assistence would really be appreciated.
>
> The action code is:
>
> public class EditProjectAction extends Action {
>
> @PersistenceUnit
> private EntityManagerFactory emf;
>
> public ActionForward execute(ActionMapping mapping, ActionForm
> form,
> HttpServletRequest request, HttpServletResponse response)
> throws Exception {
> Project project = (Project)form;
> emf.createEntityManager().persist(project);
> return null;
> }
> }
>
> The NPE is thrown at emf.createEntityManager() (emf is null).
>
> Cheers
> Adam
>
> ------------------------------------------------------------------------
> On Yahoo!7
> Socceroos Central: Latest news, schedule, blogs and videos.
> <http://us.rd.yahoo.com/mail/tag/**h...m%2Ffootball%2F>
>
>
>
| |
| Craig McClanahan 2006-06-05, 7:21 pm |
| On 6/5/06, hanasaki <hanasaki-4NU747/U+hhBDgjK7y7TUQ@public.gmane.org> wrote:
>
> What would be the preferred code for getting an emf instance in a
> non-container instantiated (non-injectable) object instance?
A couple of possibilities come to mind ...
* Inject the resource into a container-instantiated object
(like a servlet), and modify your framework to copy values
in when it creates, say, an Action instance in the Struts case.
BIG PROBLEM: thread safety ... the servlet instance is shared
across all threads, so the variable would get stomped on if there
was more than one simultaneous request.
* Modify your framework to scan for the injection annotations itself
when it creates new object instances, and perform the same kinds
of injection that the container can do.
* Use a dependency injection framework that knows how to initialize
properties from JNDI lookups, and configure it to look up the
corresponding resources.
* Use the managed beans portion of JSF (even if you are not using
the rest of it) as a dependency injection framework, and let the
container go ahead and do the injection for you. (If you're using
an additional framework, it would need to be modified to use
managed beans to create object instances, instead of doing
Class.newInstance() things itself).
Craig
Craig McClanahan wrote:
> NullPointerException.
> appreciated.
> response)
> ------------------------------------------------------------------------
> http://us.rd.yahoo.com/mail/tag/**h...m%2Ffootball%2F
>
|
|
|
|
|