user3677469
user3677469

Reputation: 15

getting value from other class

I want to get the ID number that user use to log in to only display data that is related to this user ID, if it make sense. This is my code for the index bean(index.java), just getting the ID from a simple inputText:

    private String id;

    public String getid() {
    return id;
}

public void setid(String id) {
    this.id = id;
}

and this is the code to user it(class table)

public class table {
    index i = new index();
    final String pNo=i.getid();
public List<index> getMyList()
{
     List<index> list = new ArrayList<index>();
     PreparedStatement pstmt = null;
     Connection con = null;
     ResultSet rs = null;
     try
     {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/try", "root", "");

        String sql = "SELECT task.tNo, clientcase.name "+
        "FROM clientcase join task on clientcase.cNo = task.cNo "+
        "Where task.responsiblePNo = ? AND (task.statue='opened'Or task.statue= 'pending')";
        pstmt= con.prepareStatement(sql); 
        pstmt.setString(1, pNo);
        rs= pstmt.executeQuery(); 
        while (rs.next())
        {

            i.settNo(rs.getString("task.tNo"));
            i.setCompanyName(rs.getString("clientcase.name"));

            list.add(i);
            } 
        }
        catch(Exception e)
        {}

I also have a jsf page haveing this code to get id, if this make any sense:

     <h:inputText id="personnelID" value ="#{index.id}"/>

My problem is I can get the ID number. If I replace the get method in table class to a real number, everything works fine. But now it just returns null. Can someone help me?

Upvotes: 0

Views: 66

Answers (4)

j3ny4
j3ny4

Reputation: 441

When you create a new index instance

index i = new index();

It's created with no value in id member. You should initialize the object properly or get supplied with an initialized one prior this line:

final String pNo=i.getid();

Upvotes: 0

herrlock
herrlock

Reputation: 1454

Do you use

setid(String)

anywhere? Otherwise id has its default value, which is null.

Upvotes: 0

Rahul
Rahul

Reputation: 77846

I would have a constructor in your index class and set the Id there like

public class index
{
    private String id;

    public String getid() {
    return id;
}

public void setid(String id) {
    this.id = id;
}

public index(string Id)
{
  this.setid(Id);
}
}

Then in your table class you can instantiate index like below; in that way it will make sure that ID value is set and you don't get NULL if first time you are creating a object of index and trying to get ID value

public class table {
index i = new index("1234");
final String pNo=i.getid();

Upvotes: 0

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79807

The code

index i = new index();
final String pNo=i.getid();

is being run when the table is first created, and presumably this sets pNo to null. But after that, you can't change it in any way. You probably want a constructor for table that allows you to set pNo, so that you can pass in the value from the field.

Upvotes: 1

Related Questions