我有一个sessionScoped Bean,需要在其他bean中注入。
@SessionScoped
@ManagedBean(eager = true, name="signBundle")
public class SignBundleBean implements Serializable{
private ArrayList<Signing> selectedDownloads;
private ArrayList<Signing> selectedSignings;
private ArrayList<Signing> signings = new ArrayList<>();
private ArrayList<Signing> downloads = new ArrayList<>();
private ArrayList<String> parameterColumns = new ArrayList<>();
private int maxParameters = 0;
我正在尝试将此bean注入其他bean。
@ManagedBean
@SessionScoped
public class FileUploadView implements Serializable {
@ManagedProperty(value = "#{signBundle}")
private SignBundleBean signBundleBean;
static String codesignRoot = InitialisationHelper.getCodesignRoot();
transient Map<String, Object> sessionMap;
ArrayList<Signing> signings = signBundleBean.getSignings();
我遇到异常,因为signBundle未初始化且为null。
Cant instantiate class: com.oracle.ccss.web.beans.FileUploadView.
javax.faces.FacesException: Cant instantiate class: com.oracle.ccss.web.beans.FileUploadView.
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:84)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
...
Caused by: com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.oracle.ccss.web.beans.FileUploadView.
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:191)
at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:100)
at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
...
Caused by: java.lang.NullPointerException
at com.oracle.ccss.web.beans.FileUploadView.<init>(FileUploadView.java:43)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.Class.newInstance(Class.java:374)
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:186)
答案 0 :(得分:1)
at com.oracle.ccss.web.beans.FileUploadView.<init>(FileUploadView.java:43)
正如<init>
暗示的那样,NullPointerException
是从FileUploadView
bean的构造函数抛出的。您似乎在构造函数中执行以下操作:
public FileUploadView() {
signBundleBean.doSomething();
}
您似乎期望Java能够以某种方式执行以下操作:
FileUploadView fileUploadView;
fileUploadView.setSignBundleBean(signBundle);
fileUploadView = new FileUploadView();
这是不真实的。它的工作原理如下:
FileUploadView fileUploadView;
fileUploadView = new FileUploadView();
fileUploadView.setSignBundleBean(signBundle);
如果您打算在构建后直接使用signBundle
,那么您应该使用@PostConstruct
带注释的方法来完成工作。
@PostConstruct
public void init() {
signBundleBean.doSomething();
}
JSF会在构建和托管属性设置后自动直接调用它。