0
votes

I have a XHTML page which is using JSF. On this page I have an input form which is used to gather the parameters required to call an Add() method in my managed bean class called AnimalBeanTwo. When I click the add button to call the method, I receive an error ...

value="#{animalBeanTwo.animal.name}": Target Unreachable, 'animal' returned null.

The JSF/xhtml form looks like this:

    <h:form>
<table id ="addRecordTable">
<tr>
<td><h:outputText value="Enter Name: " /></td>
<td><h:inputText value="#{animalBeanTwo.animal.name}" id="name" label="name" required="true" requiredMessage="Name is required.">
<f:validateLength minimum="2" maximum="15"></f:validateLength>
</h:inputText>
</td>
</tr>
<tr>
<td><h:outputText value="Enter Age: "/></td>
<td><h:inputText value="#{animalBeanTwo.animal.age}" id="age" label="age" required="true" requiredMessage="Age is required."></h:inputText></td>
</tr>
<tr>
<td><h:outputText value="Enter Type : " /></td>
<td><h:inputText value="#{animalBeanTwo.animal.type}" id="type" label="type" required="true" requiredMessage="type is required.">
<f:validateLength minimum="2" maximum="15"></f:validateLength>
</h:inputText>
</td>
</tr>

<tr>
<td><h:outputText value="Enter Weight : " /></td>
<td><h:inputText value="#{animalBeanTwo.animal.weight}" id="weight" label="weight" required="true" requiredMessage="Weight is required.">
<f:validateLength minimum="2" maximum="15"></f:validateLength>
</h:inputText>
</td>
</tr>

<tr>
<td></td>
<td><h:commandButton value="Add"  action="#{animalBeanTwo.add}"> 

</h:commandButton></td>
</tr>
</table>
</h:form>

My managed bean is as follows:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

@ManagedBean
public class AnimalBeanTwo {


    @ManagedProperty(value = "#{animalTwo}")
    protected AnimalTwo animal;

    public AnimalTwo getAnimal() {
        return animal;
    }

    public void setAnimal(AnimalTwo animal) {
        this.animal = animal;
    }

    public List<AnimalTwo> getAnimals() {

        List<AnimalTwo> animals = new ArrayList<AnimalTwo>();
        ResultSet rs = null;
        PreparedStatement pst = null;

        Connection con = null;

        try {

            Class.forName("com.ibm.db2.jcc.DB2Driver");
            String url = "jdbc:db2://uk01dv11db.travel.oag.com:60080/SAMPLE";
            con = DriverManager.getConnection(url,"","");
            System.out.println("Connection completed (Select All).");
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        String stm = "SELECT ID,Name, Type, Weight,Age, ID FROM Animals";

        try {
            pst = con.prepareStatement(stm);
            pst.executeQuery();
            rs = pst.getResultSet();

            while (rs.next()) {
                int age = rs.getInt("Age");
                String type = rs.getString("Type");
                String name = rs.getString("Name");
                int id = rs.getInt("ID");
                int weight = rs.getInt("Weight");
                AnimalTwo a1 = new AnimalTwo(id, type, name, age, weight);
                animals.add(a1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return animals;

    }

    public void add() {


        PreparedStatement ps = null;
        Connection con = null;
        String url = "jdbc:db2://uk01dv11db.travel.oag.com:60080/SAMPLE";
        try {
            Class.forName("com.ibm.db2.jcc.DB2Driver");
            con = DriverManager.getConnection(url, "", "");
            String sql = "INSERT INTO Animals(Age, Type, Name, Weight) VALUES(?,?,?,?)";
            ps = con.prepareStatement(sql);
            ps.setInt(1, animal.getAge());
            ps.setString(2, animal.getType());
            ps.setString(3, animal.getName());
            ps.setInt(4, animal.getWeight());

            ps.executeUpdate();
            System.out.println("Data Added Successfully Into Database");
        } catch (Exception e) {
            System.out.println(e);
            System.out.println("exception here");

        }
    }
}

This is my AnimalTwo class:

package com.oag.jsf;

public class AnimalTwo 
{
    private int id;
    private String type;
    private String name;
    private int age;
    private int weight;


    public AnimalTwo(int id, String type, String name, int age, int weight) {
        super();
        this.id = id;
        this.type = type;
        this.name = name;
        this.age = age;
        this.weight = weight;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    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;
    }
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }
    @Override
    public String toString() {
        return "DB2Animal [id=" + id + ", type=" + type + ", name=" + name
                + ", age=" + age + ", weight=" + weight + "]";
    }
}

I can provide any further code if needed. Any suggestions are appreciated. Thanks.

1

1 Answers

1
votes

Try to initialize the AnimalTwo object in its getter method like this :-

write new empty constructor in AnimalTwo class

public AnimalTwo() {
// empty constructor   
}

and in your managedBean edit getter method like this :-

public AnimalTwo getAnimal() {
    if(animal == null){
       animal = new animal();
    }
    return animal;
}

i hope this fix it.