0

我有一个带有动物园对象数组列表的动物园类,在动物园中有动物列表。访问动物属性很酷,但访问 membermethods 会导致异常。

//Zoos 类,对象为 Animals,Trees

@ManagedBean
public @SessionScoped class Zoos {
    private String name;
    private String description;

        private Animals animals=new Animals ();

    public void setAnimals(Animals animals) {
        this.Animals = Animals;
    }

    public Animals getAnimals() {
        return animals;
    }
        //All the getters and setters
}

//动物类

public class Animals implements Serializable {

    private static final long serialVersionUID = 1L;
    private String name; 
    private String breed;
        static final ArrayList<Animal> apbp 
    = new ArrayList<Animal>(); 

       //Getters and setters...
       //Add animal in the list
    public String addAnimal()
        { 
            Animal abp = new Animal(name);
            abp.setbreed(breed);
            apbp.add(abp);
        name="";
        natPoolName="";
        defaultPublicFlag=false;
        defaultShareableFlag=false;
        enabled=false;
        return "success";

        }

}

//动物类

public class Animal{

    private String name; 
    private String breed;

       //Getters and setters

}

将 Animals 类的属性调用为 zoos.animal.name效果很好。

**但是从 *.xhtml 调用成员函数 zoos.animals.addAnimal 为

<h:commandButton id="ok" value="Ok" action="#{zoos.animals.addAnimal}" oncomplete="#{rich:element('mypanel')}.style.display='none'; return false;" />

这不起作用并给出异常为**

javax.servlet.ServletException
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:325)
root cause
java.lang.NullPointerException
    com.sun.faces.application.view.StateManagementStrategyImpl$4.invokeContextCallback(StateManagementStrategyImpl.java:289)
    javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1253)
    javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:672)
    com.sun.faces.application.view.StateManagementStrategyImpl.restoreView(StateManagementStrategyImpl.java:284)
    com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:177)
    com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java:131)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:430)
    com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:143)
    javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:288)
    com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:199)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)

我无法弄清楚问题所在。

4

1 回答 1

0

我认为 NullPointerException 用于将 jsf 生命周期#{zoos.animals.addAnimal} 视为动物 setter 和 getter ,所以我更喜欢在你的 bean 中添加方法并直接使用它:

@ManagedBean
public @SessionScoped class Zoos {
    private String name;
    private String description;

        private Animals animals=new Animals ();

    public void setAnimals(Animals animals) {
        this.Animals = Animals;
    }

    public Animals getAnimals() {
        return animals;
    }

    public void addAnimalInZoo(){
        if(getAnimals()!=null) {
                getAnimals().addAnimal();
        }
    }
        //All the getters and setters
}

并在您的页面中addAnimalInZoo()直接使用:

<h:commandButton id="ok" value="Ok" action="#{zoos.addAnimalInZoo}" oncomplete="#{rich:element('mypanel')}.style.display='none'; return false;" />
于 2013-04-01T09:18:31.093 回答