简体   繁体   English

EJB空点异常

[英]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). 我有一个带接口的类和一个新类,我将其称为new ojbClass(params)-其中包含一个ejb调用(下面的代码)。 When it get's to call the ejb from that class, i get null point exception. 当从该类中调用ejb时,我得到了空点异常。

Class calling EJB method inside 类内部调用EJB方法

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 EJB接口

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

EJB Class EJB类

@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. 您尝试的操作不起作用: 您不能在DTO中注入EJB,因为它不是托管类。

Either refactor your dependency injection or pass the ProfileRemoteBean to the ProfileDTO via the constructor. 重构依赖项注入,或通过构造函数将ProfileRemoteBean传递给ProfileDTO

Your code seems ok, the NPE does not come from your method ProfileRemoteBean.addProfile(Profile profile) instead? 您的代码看起来还可以,NPE不是来自您的方法ProfileRemoteBean.addProfile(Profile profile)吗?

Could it be possible to paste it? 可以粘贴吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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