EJB Null point exception

347 views Asked by At

I have a class with interface and a new class for which I call new ojbClass(params) - which contains an ejb call (code below). When it get's to call the ejb from that class, i get null point exception.

Class calling EJB method inside

public class ProfileDTO {

    private Profile profileEntity;

    @EJB
    private ProfileRemoteBean profileBean; //remote bean is interface name

    public ProfileDTO(String firstName, String lastName, Date birthDate, String gender, String country, String district, String city, String identificationNo, String idCardNo, String phone) {

        createProfile();

    }

    public void createProfileEntity() {

        Profile profileEntity = new Profile();

        //deleted code for stackoverflow
        //...................................
        this.profileEntity = profileEntity;
    }

    public Profile getProfileEntity(){
        return this.profileEntity;
    }

    private void createProfile() {
        createProfileEntity();
        profileBean.addProfile(getProfileEntity()); // profile bean null point here
    }

}

EJB Interface

@Remote
public interface ProfileRemoteBean {
    public List<Profile> getProfile();
}

EJB Class

@Stateless
public class ProfileBean implements ProfileRemoteBean,Serializable{

    @PersistenceContext(unitName = "com.ulbs.admission.core_AdmissionCoreDBEJB_ejb_1.0-SNAPSHOTPU", type = PersistenceContextType.TRANSACTION)
    private EntityManager entityManager;



    @Override
    public List<Profile> getProfile() {
     TypedQuery<Profile> query = entityManager.createNamedQuery("Profile.findAll", Profile.class);
     return query.getResultList();
    }
}

Can you please give me some hints or a solution?

Thanks!

2

There are 2 answers

0
unwichtich On BEST ANSWER

What you are trying is not supposed to work: You can't inject an EJB in a DTO because it is not a managed class.

Either refactor your dependency injection or pass the ProfileRemoteBean to the ProfileDTO via the constructor.

1
devjiel On

Your code seems ok, the NPE does not come from your method ProfileRemoteBean.addProfile(Profile profile) instead?

Could it be possible to paste it?