2015-04-04 162 views
0

大家好我是javaweb的新手,我嘗試創建我的第一個javaweb項目,按照一些教程進行課程。但是,當某個動作被調用時,我收到一個錯誤。有沒有人可以幫忙!下面是我做的:如何解決Target Unreachable,'null'?

Person.java

package org.javaeesample.entities; 
import java.io.Serializable; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 

@Entity 
public class Person implements Serializable { 
    @Id @GeneratedValue 
    private Long id; 

    private String name; 
    private int age; 

    public Person(){   
    } 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getAge() { 
     return age; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 
} 

PersonEjbDao.java

package org.javaeesample.ejbdao; 

import java.util.List; 
import javax.ejb.Stateless; 
import javax.persistence.EntityManager; 
import javax.persistence.PersistenceContext; 
import javax.persistence.criteria.CriteriaBuilder; 
import javax.persistence.criteria.CriteriaQuery; 
import javax.persistence.criteria.Root; 
import org.javaeesample.entities.Person; 

@Stateless 
public class PersonEjbDao { 
    @PersistenceContext 
    EntityManager em; 
    //private Person pers; 
    public List<Person> listPerson(){ 
     CriteriaBuilder cb = em.getCriteriaBuilder(); 
     CriteriaQuery<Person> cq = cb.createQuery(Person.class); 
     Root<Person> person = cq.from(Person.class); 
     cq.select(person); 
     return em.createQuery(cq).getResultList(); 
    } 

    public Person findPerson(long pid){ 
     return em.find(Person.class, pid); 
    } 

    public void createPerson(Person pers){ 
     Person p = new Person(); 
     p.setName(pers.getName()); 
     p.setAge(pers.getAge()); 
     em.persist(p); 
    } 
} 

FriendBean.java

package org.javaeesample.jsf; 

import java.util.List; 
import javax.enterprise.context.RequestScoped; 
import javax.inject.Inject; 
import javax.inject.Named; 
import org.javaeesample.ejbdao.PersonEjbDao; 
import org.javaeesample.entities.Person; 

@Named(value = "personBean") 
@RequestScoped 
public class PersonBean { 
    private Person person; 
    private Long pid; 
    private List<Person> listPerson; 
    @Inject 
    PersonEjbDao personDao; 

    public List<Person> getPersons(){ 
     if (listPerson==null) { 
      listPerson = personDao.listPerson(); 
     } 
     return listPerson; 
    } 

    public void personDetails(){ 
     person = new Person(); 
     person = (Person) personDao.findPerson(pid); 
    } 

    public void createPerson(){ 
     personDao.createPerson(person); 
    } 

    public Person getPerson() { 
     return person; 
    } 

    public void setPerson(Person person) { 
     this.person = person; 
    } 

    public Long getPid() { 
     return pid; 
    } 

    public void setPid(Long pid) { 
     this.pid = pid; 
    } 

} 

show.xhtml

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:ui="http://xmlns.jcp.org/jsf/facelets" 
     xmlns:h="http://xmlns.jcp.org/jsf/html" 
     xmlns:f="http://xmlns.jcp.org/jsf/core"> 
    <body> 

     <ui:composition template="./WEB-INF/template.xhtml"> 

      <ui:define name="content"> 
       <h:form> 
       <h:dataTable value="#{personBean.persons}" var="p"> 
        <h:column> 
         <f:facet name="header">Name</f:facet> 
         <h:link value="#{p.name}" outcome="details"> 
          <f:param name="pid" value="#{p.id}"/> 
         </h:link> 
        </h:column> 

        <h:column> 
         <f:facet name="header">Age</f:facet> 
         #{p.age} 
        </h:column> 
       </h:dataTable> 

       <h:commandButton id="create" action="create" value="Create"/> 
       </h:form> 
      </ui:define> 

     </ui:composition> 

    </body> 
</html> 

create.xhtml

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:ui="http://xmlns.jcp.org/jsf/facelets" 
     xmlns:h="http://xmlns.jcp.org/jsf/html"> 

    <body> 

     <ui:composition template="./WEB-INF/template.xhtml"> 

      <ui:define name="content"> 
       <h:form> 
        <h:panelGrid columns="3" 
          captionClass="rightalign,leftalign,leftalign"> 
         <h:outputLabel value="Name:" for="name"/> 
         <h:inputText id="name" label="Name" 
           required="true" 
           value="#{personBean.person.name}"/> 
         <h:message for="name"/> 
         <h:outputLabel value="Age:" for="age"/> 
         <h:inputText id="age" label="Age" 
           size="2" 
           value="#{personBean.person.age}"/> 
         <h:message for="age"/> 
         <h:commandButton id="save" value="Save" 
            action="list" actionListener="#{personBean.createPerson}"/> 
        </h:panelGrid> 
       </h:form> 
      </ui:define> 
     </ui:composition> 

    </body> 
</html> 

details.xhtml

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:ui="http://xmlns.jcp.org/jsf/facelets" 
     xmlns:f="http://xmlns.jcp.org/jsf/core" 
     xmlns:h="http://xmlns.jcp.org/jsf/html"> 

    <body> 

     <ui:composition template="./WEB-INF/template.xhtml"> 
      <ui:define name="content"> 
       <f:metadata> 
        <f:viewParam name="pid" value="#{personBean.pid}"/> 
        <f:event type="javax.faces.event.PreRenderComponentEvent" listener="#{personBean.personDetails()}"/> 
       </f:metadata> 

       <h:form> 
        <h:panelGrid columns="2" 
          columnClasses="rightalign,leftalign,leftalign"> 
         <h:outputText value="Name:"/> 
         <h:outputText value="#{personBean.person.name}" /> 
         <h:outputText value="Age:"/> 
         <h:outputText value="#{personBean.person.age}"/> 
        </h:panelGrid> 
       </h:form> 
      </ui:define> 
     </ui:composition> 

    </body> 
</html> 

當試圖運行我的項目所發生的事情是:
1當我點擊th從show.xhtml E鏈路

<h:link value="#{p.name}" outcome="details"> 
    <f:param name="pid" value="#{p.id}"/> 
</h:link> 

它說:顯示java.lang.NullPointerException

java.lang.NullPointerException 
    at org.javaeesample.jsf.PersonBean.personDetails(PersonBean.java:37) 
    at org.javaeesample.jsf.PersonBean$Proxy$_$$_WeldClientProxy.personDetails(Unknown Source) 
    ... 

,但是,從URL它顯示:
http://localhost:8080/javaeesample/faces/details.xhtml?pid=1

  • 當我點擊'保存'按鈕在create.xhtml這裏是我收到的錯誤:
  • 中出現錯誤:

    /create.xhtml @18,73 value="#{personBean.person.name}": Target Unreachable,'null' returned null 
    

    我擴大堆棧跟蹤,並將其顯示

    javax.el.PropertyNotFoundException: /create.xhtml @18,73 value="#{personBean.person.name}": Target Unreachable, 'null' returned null 
        at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100) 
        at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95) 
    

    我不知道是什麼讓我personBean.person.name空。

    任何幫助非常感謝。

    +0

    感謝先生的答案,但有必要做@PostConstruct和地方我應該把這個註釋嗎?那麼我的create.xhtml是什麼導致了錯誤呢? – 2015-04-04 11:00:59

    回答

    0

    由於Tiny似乎不想寫一個答案,我會去做。基地這個答案部分是他的評論曰:

    目標不可達,「空」返回null:要麼#{personBean.person}#{personBean}null。我聞到personnull。您正在嘗試 預填充personDetails()之內的人員。爲什麼使用<f:event type="javax.faces.event.PreRenderComponentEvent" listener="#{personBean.personDetails()}"/>初始化 人?在更好的初始化位置 - 在註釋的方法 中,您最好有 預填充private Person person;(和private List<Person> listPerson;,這是您在獲取方法 內不必要地初始化的內容)。

    現在對

    javax.el.PropertyNotFoundException: /create.xhtml @18,73 value="#{personBean.person.name}": 
        Target Unreachable, 'null' returned null 
    

    這個錯誤多一點的解釋是指:「無論是#{personBean.person}#{personBean}爲空(見上文)我用微小的是完全的假設,person。是罪魁禍首

    這樣做的根本原因是,當你用來初始化Person運行的監聽器時,xhtml文件是alre ady完全解析。在聽衆有機會改變person之前,它的屬性應該已被訪問。

    這會導致您的錯誤。對此的修復相當簡單:

    不要讓person永遠爲空。 用渴望的初始化,您可以防止這一點,並讓你的PersonBean如下所示:

    @RequestScoped 
    public class PersonBean { 
        private Person person = new Person(); 
        private Long pid = 0l; 
        private List<Person> listPerson = new ArrayList<>(); 
        // carry on with your code from here 
    

    這允許你刪除延遲初始化部分在您的getter和應該解決您的問題。

    這另一種方法是(爲Tinyhis comment已經提到),使用具有@PostConstruct註釋的方法,像這樣:

    @PostConstruct 
    public void initialize() { 
        this.person = (Person) personDao.findPerson(pid); 
        this.personList = personDao.listPerson(); 
    } 
    
    +0

    爲什麼它仍然沒有輸出?我嘗試了你的建議,但仍然返回null。僅在我的show.xhtml上名稱:和年齡:顯示,並在我的create.xhtml仍然返回到目標無法緩解。我正在使用Netbeans 8.0和glassfish4和jsf 2.2。 – 2015-04-17 03:43:49

    相關問題