简体   繁体   中英

EJB Null point exception

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!

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.

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

Could it be possible to paste it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM