2012-07-16 67 views
5

我知道有一些錯誤的managedBean,但我不能弄清楚它是什麼javax.el.PropertyNotFoundException:目標不可達,「空」返回null外鍵參考

當我嘗試departmentManagedBean.department.faculty.facultyName從JSF頁面,它提供了空指針這樣的:

javax.el.PropertyNotFoundException:/administrator/department/DepartmentRegistration.xhtml @ 30260值= 「#{} departmentRequestBean.department.faculty.facultyName」:目標不可達, 'null'返回null

javax.el.PropertyNotFoundException: /administrator/department/DepartmentRegistration.xhtml @30,260 value="#{departmentRequestBean.department.faculty.facultyName}": Target Unreachable, 'null' returned null 
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100) 
at org.richfaces.component.util.InputUtils.findConverter(InputUtils.java:123) 
at org.richfaces.component.util.InputUtils$1.getConverterByProperty(InputUtils.java:56) 
at org.richfaces.component.util.InputUtils.getConvertedValue(InputUtils.java:148) 
at org.richfaces.component.util.InputUtils.getConvertedValue(InputUtils.java:137) 
at org.richfaces.renderkit.InputRendererBase.getConvertedValue(InputRendererBase.java:50) 
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030) 
at javax.faces.component.UIInput.validate(UIInput.java:960) 
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233) 
at javax.faces.component.UIInput.processValidators(UIInput.java:698) 
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 
at javax.faces.component.UIForm.processValidators(UIForm.java:253) 
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) 
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172) 
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76) 
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) 
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) 
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) 
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849) 
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746) 
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045) 
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228) 
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
at java.lang.Thread.run(Thread.java:636) 

public class Department implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @Id 
    @Basic(optional = false) 
    @NotNull 
    @Column(name = "DepartmentID") 
    private Integer departmentID; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 255) 
    @Column(name = "DepartmentName") 
    private String departmentName; 
    @JoinColumn(name = "FacultyName", referencedColumnName = "FacultyName") 
    @ManyToOne(optional = false, fetch = FetchType.EAGER) 
    public Faculty faculty; 

    public Faculty getFaculty() { 
     return faculty; 
    } 

    public void setFaculty(Faculty faculty) { 
     this.faculty = faculty; 
    } 
    public Department() { 

    } 

    public Department(Integer departmentID) { 
     this.departmentID = departmentID; 
    } 

    public Department(Integer departmentID, String departmentName) { 
     this.departmentID = departmentID; 
     this.departmentName = departmentName; 
    } 

    public Integer getDepartmentID() { 
     return departmentID; 
    } 

    public void setDepartmentID(Integer departmentID) { 
     this.departmentID = departmentID; 
    } 

    public String getDepartmentName() { 
     return departmentName; 
    } 

    public void setDepartmentName(String departmentName) { 
     this.departmentName = departmentName; 
    } 
} 

public class DepartmentSessionBean { 
    @PersistenceContext(unitName = "ApplicationPU") 
    private EntityManager em; 

    public void persist(Department department) {  
     em.persist(department); 
    } 
} 

public class Faculty implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @Id 
    @Basic(optional = false) 
    @NotNull 
    @Column(name = "FacultyID") 
    private Integer facultyID; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 255) 
    @Column(name = "FacultyName") 
    private String facultyName; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "faculty", fetch = FetchType.EAGER) 
    private List<Department> departmentList; 

    public Faculty() { 
    } 

    public Faculty(Integer facultyID) { 
     this.facultyID = facultyID; 
    } 

    public Faculty(Integer facultyID, String facultyName) { 
     this.facultyID = facultyID; 
     this.facultyName = facultyName; 
    } 

    public Integer getFacultyID() { 
     return facultyID; 
    } 

    public void setFacultyID(Integer facultyID) { 
     this.facultyID = facultyID; 
    } 

    public String getFacultyName() { 
     return facultyName; 
    } 

    public void setFacultyName(String facultyName) { 
     this.facultyName = facultyName; 
    } 

    public List<Department> getDepartmentList() { 
     return departmentList; 
    } 

    public void setDepartmentList(List<Department> departmentList) { 
     this.departmentList = departmentList; 
    } 
} 

這是我需要請一個解決方案ManagedBean:

@ViewScoped public class DepartmentRequestBean { 
@EJB 
private DepartmentSessionBean departmentSessionBean; 
private Department department = new Department(); 
public Department getDepartment() { 
    return department; 
} 

public void setDepartment(Department department) { 
    this.department = department; 
} 


public void save(){ 


     departmentSessionBean.persist(department); 

} 

public DepartmentRequestBean() { 
    this.faculty = new Faculty(); 
}} 

回答

8

值= 「#{departmentRequestBean.department.faculty.facultyName}」:目標不可達, '空' 返回null

這基本上意味着,下列值之一

  • #{departmentRequestBean.department}
  • #{departmentRequestBean.department.faculty}

返回null在處理表單提交期間。由於您自己明確創建了Department,因此它只能表示Faculty仍然是null

要正確地解決它,在DepartmentRequestBean

private Department department = new Department(); 

替代由

private Department department; 

@PostConstruct 
public void init() { 
    department = new Department(); 
    department.setFaculty(new Faculty()); 
} 
0

像這樣

public Department getDepartment() { 
if(department == null){ 
department = (Department)super.getInstance(Department.class); 
} 
return department; 
} 

這將創建一個新的部門的支持bean初始化你的部門級如果對象變爲空,則返回對象

相關問題