2015-04-04 166 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

    相关问题