Java EE - Entity Manager always says null

A

Aravind

Hello,
I am new to java EE - Im using Java5 bundled with Netbeans and java App
server and the Toplink Db.

I created entity classes for a table in db and a stateless session bean
for the enity classes. (Everything was automatically done in Netbeans).
Now I have a app client which calls the session bean (remote
interface).

The problem is that whenever the bean tries to acess the database, I
get a NullPointerException.

If I simply return a "Hello" message from the bean to the appClient it
works - that is there is no problem for my app client to access to
session bean. But when the bean tries to contact the db using the
entitymanager.find() method, then the entity manager always gives the
exception.

Am I missing something here?

Thanks,
--Aravind.

The code:
package ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class BankloginFacade implements BankloginFacadeLocal,
BankloginFacadeRemote {

@PersistenceContext
private EntityManager em;

public BankloginFacade() {
}

public void create(Banklogin banklogin) {
em.persist(banklogin);
}

public void edit(Banklogin banklogin) {
em.merge(banklogin);
}

public void destroy(Banklogin banklogin) {
em.merge(banklogin);
em.remove(banklogin);
}

public Banklogin find(Object pk) {
//***********ERROR RAISED HERE**********
return (Banklogin) em.find(Banklogin.class, pk);
}

public List findAll() {
return em.createQuery("select object(o) from Banklogin as
o").getResultList();
}

Error stack trace:
WARNING: ACC003: Application threw an exception.
java.lang.NullPointerException
at ejb.BankloginFacade.find(BankloginFacade.java:46)
at bankapp.Main.main(Main.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at
com.sun.enterprise.util.Utility.invokeApplicationMain(Utility.java:232)
at
com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:329)
at com.sun.enterprise.appclient.Main.main(Main.java:180)
Exception in thread "main" java.lang.RuntimeException:
java.lang.reflect.InvocationTargetException
at
com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:340)
at com.sun.enterprise.appclient.Main.main(Main.java:180)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at
com.sun.enterprise.util.Utility.invokeApplicationMain(Utility.java:232)
at
com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:329)
... 1 more
Caused by: java.lang.NullPointerException
at ejb.BankloginFacade.find(BankloginFacade.java:46)
at bankapp.Main.main(Main.java:35)
... 7 more
Java Result: 1
 
W

Wesley Hall

I created entity classes for a table in db and a stateless session bean
for the enity classes. (Everything was automatically done in Netbeans).
Now I have a app client which calls the session bean (remote
interface).

The problem is that whenever the bean tries to acess the database, I
get a NullPointerException.


It looks like your entity manager is not configured properly. There
should be a file named persistence.xml in your META-INF directory in
your ejb jar that does this. You may also need to specify unitName as a
parameter to the @PersistentContext annotation.

It is very difficult to give you specific advice, I would simply advise
that you look at the examples that come with your application server,
these should give you all the information you need.
 
A

Aravind

Thanks for the reply.

Actually, I have configured, the persistence.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="BankApp-ejbPU" transaction-type="JTA">

<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
<jta-data-source>jdbc/sample</jta-data-source>
<class>ejb.Bankaccount</class>
<class>ejb.Bankcustomer</class>
<class>ejb.Banklogin</class>
<class>ejb.Banktransaction</class>
<properties>
<property name="toplink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>


Does this give any insight to the problem?

Also, I found that another person has described the same problem - no
solution though !!
Here:
http://forum.java.sun.com/thread.jspa?threadID=786505

In Netbeans, after creating session beans for entity classes, do I need
to perform any other steps? All I did was create Entity classes from
database and then create session beans for enity classes - wouldn't
that automatically take care of database connections?

If it were a web app (servlet) then after creating the entity classes
from database, I would add the persistence unit to the servlet and then
use the entity classes. But I dont do anything with the persistence
unit now - Could that be the reason?

Please help me out. Thanks.
 
W

Wesley Hall

Please help me out. Thanks.

I will try :)

The only thing that spring immediately to mind is that your application
server doesn't know which persistent unit you are trying to inject.

Try changing your annotation so it looks like this...

@PersistenceContext(unitName = "BankApp-ejbPU")

This should tell the application server to inject the persistent unit
called BankApp-ejbPU which is what you have defined in your
persistence.xml. Hopefully this will either work (best case scenario) or
present you with a more meaningful error.
 
A

Aravind

I tried - gives me the same errors though :(

Wesley said:
I will try :)

The only thing that spring immediately to mind is that your application
server doesn't know which persistent unit you are trying to inject.

Try changing your annotation so it looks like this...

@PersistenceContext(unitName = "BankApp-ejbPU")

This should tell the application server to inject the persistent unit
called BankApp-ejbPU which is what you have defined in your
persistence.xml. Hopefully this will either work (best case scenario) or
present you with a more meaningful error.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top