Cant get EJB create method to work inside another EJB

I am using EJB 3.0 and trying to create a customer record in the database using the following method in another session EJB:

customerFacade.create(cust);

I continue to get a null pointer error on the above line, even though I have established non null values for EVERY field in the customer table.

The only things I have not done a cust.set...() method for are the related object collections with one to many relationships (such as cust.setCustAddressCollection() ).

My code works when I put in a servlet, but does not work when I call it inside another EJB. Is there some reason it will not work in the EJB?

Here is the code in the session bean that does not work:

publicvoid registerCustomer(Customer cust, Address addr){

// Declare & instatiate the necessary objects

// set up Date

Date now =new Date();

//encrypt the password

String password = cust.getLogonpassword();

encryptionBean encrypter =new encryptionBean();

int salt = encrypter.getSalt();

String encryptedPass = encrypter.encryptIt(password, salt);

// Get next available Key & increment

KeyManagerBean custKey =new KeyManagerBean();

BigInteger customerId = custKey.getNextKey("customer");

BigDecimal bdSetter =new BigDecimal("0");

int intSetter = 0;

// add the additional necessary fields

try{

cust.setCustId(customerId);

cust.setLogonpassword(encryptedPass);

cust.setSalt(String.valueOf(salt));

cust.setRegistration(now);

cust.setLastsession(now);

cust.setPasswordcreation(now);

cust.setPasswordretries(0);

cust.setField1(intSetter);

cust.setField2(intSetter);

cust.setField3(intSetter);

cust.setField4(bdSetter);

cust.setField5("");

cust.setField6("");

cust.setField7("");

cust.setField8("");

cust.setField9("");

cust.setLastorder(now);

cust.setPasswordinvalid(now);

cust.setRegistrationupdate(now);

customerFacade.create(cust);

}catch (Exception e){

System.out.println("exception adding cust in CustomerMaintenanceBean: "+e);

}

}

[3033 byte] By [Perryiera] at [2007-11-27 11:06:07]
# 1

how/where is customerFacade initialized? That's not shown anywhere in the code snippet.

ksaksa at 2007-7-29 13:13:11 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

The CustomerFacade is initialized at the top of the class code:

public class CustomerMaintenanceBean implements CustomerMaintenanceLocal {

@EJB

private CustomerFacadeLocal customerFacade;

I just gave the code for the method being called.

Perryiera at 2007-7-29 13:13:11 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

How are you invoking the CustomerMaintenanceBean.registerCustomer() method? You need to make

sure you are doing that through a CustomerMaintenanceLocal EJB reference, not by explicitly

calling new() on CustomerMaintenanceBean.If new() is called, injection won't happen.

ksaksa at 2007-7-29 13:13:11 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

In my register servlet, I declare at the top:

import highsmith.beans.CustomerMaintenanceBean;

then after performing what is necessary, I call:

// register the customer. (adds the customer record and the address records)

try {

CustomerMaintenanceBean newCustomerMB = new CustomerMaintenanceBean();

newCustomerMB.registerCustomer(newCust, address);

} catch(Exception e) {

registerMessage = "error running register customer method: "+e;

request.setAttribute("registerMsg",registerMessage);

RequestDispatcher disp = getServletContext().getRequestDispatcher("/registerForm.jsp");

disp.forward(request, response);

}

Are you saying that i need to do this instead?:

import highsmith.beans.CustomerMaintenanceLocal;

at the top, and then below:

CustomerMaintenanceLocal newCustomerMB = new CustomerMaintenanceLocal();

Perryiera at 2007-7-29 13:13:11 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

>

> Are you saying that i need to do this instead?:

>

> import

> highsmith.beans.CustomerMaintenanceLocal;

> at the top, and then below:

> CustomerMaintenanceLocal newCustomerMB = new

> CustomerMaintenanceLocal();

No, this is not how the EJB client programming model works. In EJB, the caller never

directly uses the new() operator to get an EJB reference. You always either lookup

an EJB reference or have it injected for you.

In the servlet, use

@EJB

private CustomerMaintenanceLocal newCustomerMB;

ksaksa at 2007-7-29 13:13:11 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

Thanks for your help KSAKS!

This appears to have worked. I am now getting different errors, but i seem to be past the null error.

Thanks much!

Perryiera at 2007-7-29 13:13:11 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7

Happy to help out :-)

ksaksa at 2007-7-29 13:13:11 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...