Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javabean "Property has no getter method" error.. HELP

Status
Not open for further replies.

calypso13

Technical User
Nov 5, 2004
21
0
0
US
I am trying to display data info for each user by getting the user value which is already in the session but I keep getting this error.

Exception: Error looking up property "info" in object type "test.info". Cause: Property 'info' has no getter method

WHat am I doing wrong?
Code:
//info.java
public class info {
public info(){
}
  public Collection getinfo(String user) throws SQLException {
                ArrayList list = new ArrayList();         
String sql = "SELECT ADDR where USER= ?";
                try {//connect statement
pstmt = con.prepareStatement(sql);
pstmt.setString(1, user);
rs = pstmt.executeQuery();
while(rs.next()) {
                Infobean tsel = new Infobean(rs.getString("ADDR")); 
list.add(tsel);
}
                        return list;
 
//Infobean.java
public class Infobean 
{
private String ADDR;
private String user;
public Infobean(){
}
public Infobean(String ADDR, String user){
setAddr(Infobean);
setUser(user);
}
public String getAddr(){
return ADDR;
}
public String getUser(){
return user;
}
public void setAddr(String ADDR){
this.ADDR = ADDR;
}
public void setUser(String user){
this.user= user;
}
}
 
//.jsp file
<jsp:useBean id="test" class="info" scope="request"/>
<c:forEach items="test.info" var="x">
<td>${x.ADDR}</td>
</c:forEach>
 
Hi,

The way you are calling the method inside java and using the JSTL is wrong.

Your Code ::
Code:
//info.java [COLOR=red]Info.java // always the class name should start with capital letter[/color]
public class info {
public info(){
}
  public Collection getinfo(String user) throws SQLException {
                ArrayList list = new ArrayList();         
String sql = "SELECT ADDR where USER= ?"; [COLOR=red]missing FROM clause [/color]
                try {//connect statement
pstmt = con.prepareStatement(sql);
pstmt.setString(1, user);
rs = pstmt.executeQuery();
while(rs.next()) {
                Infobean tsel = new Infobean(rs.getString("ADDR")); [COLOR=red]The Constructor of InfoBean is taking two string parameters or no parameters [/color]
  list.add(tsel);
 }
 return list;


 
//Infobean.java
public class Infobean
{
private String ADDR;[COLOR=red] use small letters if it is a two word letter then first letter in the word will be small letter and the second word will start with capital letter [/color]
private String user;
public Infobean(){
}
public Infobean(String ADDR, String user){
setAddr(Infobean);
setUser(user);
}
public String getAddr(){
return ADDR;
}
public String getUser(){
return user;
}
public void setAddr(String ADDR){
this.ADDR = ADDR;
}
public void setUser(String user){
this.user= user;
}
}
 
//.jsp file
<jsp:useBean id="test" class="info" scope="request"/>
<c:forEach items="test.info" var="x"> [COLOR=red]should use ${test.info} but getinfo method need a string as an arg [/color]
<td>${x.ADDR}</td>
</c:forEach>

Your code should be some thing like this

Code:
//info.java
public class Info {

 private String user;
 
 public Info(){
 }

 public void setUser(String user)
  {
    this.user = user;
  }


  public String getUser()
  {
    return user;
  }
  public Collection getInfo() throws SQLException {
                ArrayList list = new ArrayList();         
String sql = "SELECT ADDR FROM <TABLE NAME> where USER= ?";
                try {//connect statement
pstmt = con.prepareStatement(sql);
pstmt.setString(1, this.user);
rs = pstmt.executeQuery();
while(rs.next()) {
   Infobean tsel = new  Infobean(rs.getString("ADDR"),this.user);
   list.add(tsel);
  }// end while
  return list;
 } // end getInfo
}
 
//Infobean.java
public class InfoBean {
    private String addr;
    private String user;

    public InfoBean() {
    }

    public InfoBean(String addr, String user) {
        this.addr = addr;
        this.user = user;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }
}
 
//.jsp file
<jsp:useBean id="test" class="info" scope="request"/>
<jsp:setProperty name="test" property="user"  value="userValue"/>
<c:forEach items="${test.info}" var="x">
<td><c:out value="${x.addr}"/></td>
</c:forEach>

Hope this will help you.

Cheers
Venu
 
Thanks for the help.
But why is the setter and getter in both Info.java and Infobean.java?

Code:
 private String user;
 
 public Info(){
 }

 public void setUser(String user)
  {
    this.user = user;
  }


  public String getUser()
  {
    return user;
  }
 
Hi,

In Info() class I have added these methods coz you need user a the parameter to query the database and I am not sure we can call a method with a parameter using JSTL. So I have set the property user using jsp:setProperty.

Thanks
Venu
 
Am still getting the same error as before.


EXCEPTION: javax.servlet.ServletException: Error looking up property "info" in object type "info".

ROOTCAUSE: [.LookupUtil] Error looking up property "info" in object type "info". Cause: Unknown property 'info'
 
Hi,

Is the method name you are calling in Info class is getInfo();
If not just make sure it has.

can you copy the code of Info class and the jsp where you are calling this?

Cheers
Venu
 
I ended up adding the setter and getter method for "addr" in my info.java because everytime I added this
<td>${t1.addr}</td>, it also had an error that it couldnt find the "addr" getter method.


Code:
//info.java
package database;
public class Info{
 private String user;
 
public Info(){
}
 public void setuser(String user)
  {
    this.user = user;
  }

  public String getuser()
  {
    return user;
  }
 public void setuser(String addr)
  {
    this.addr = addr;
  }
  public String getaddr()
  {
    return addr;
  }
   public Collection getInfo() throws NamingException, SQLException {
                ArrayList list = new ArrayList();
                InitialContext ctx = new InitialContext();
                //OTHER CONNECTION STATEMENTS
String sql = "SELECT ADDR FROM TABLENAME WHERE user=' ?'"; 
                try {
                        conn = ds.getConnection();

pstmt = conn.prepareStatement(sql);
pstmt.setString(1, this.user);

rs = pstmt.executeQuery();
while(rs.next()) {
        InfoBean tsel = new InfoBean(rs.getString("addr"), this.user);
list.add(tsel);
}
                        return list;
} finally {
                       //CLOSE pstmt, conn, and rs statements
                }
        }

}

//InfoBean.java
package database;


public class infoBean
{
private String addr;
private String user;


public infoBean(){
}

public infoBean(String addr, String user)
{
this.addr = addr;
this.user = user;
}

public String getaddr()
{
return addr;
}
public String getuser(){
return user;
}

public void setaddr(String addr){
this.addr = addr;
}
public void setuser(String user){
this.user = user;
}
}

//info.jsp

<jsp:useBean id="test" class="database.Info" scope="request">
<jsp:setProperty name="test" property="user" value="${param.user}"/>
</jsp:useBean>

<c:forEach items="${test.Info}" var="t1">
<td>${t1.addr}</td<>
</c:forEach>
 
Hi,

1.Rename infoBean class to InfoBean and recompile InfoBean
2.In JSP change
Code:
<jsp:useBean id="test" class="database.Info" scope="request"/>
<jsp:setProperty name="test" property="user" param="user"/>
3.In the JSP change to <c:forEach items="${test.info}" var="t1">

Try again
Cheers
Venu
 
I really want to thank you for all your help. I tried the above and now I get the following errors:
Code:
[i]javax.servlet.ServletException:javax.servlet.jsp.el.ELException: An error occurred while getting property "info" from an instance of class database.Info[/i]

root cause
java.sql.SQLException: Invalid parameter number.
	com.basis.jdbc.a.a(a.java:113)
	com.basis.jdbc.i.setString(i.java:333)
	org.apache.commons.dbcp.DelegatingPreparedStatement.setString(DelegatingPreparedStatement.java:131)
	database.Hist.getHist(Hist.java:37)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	java.lang.reflect.Method.invoke(Method.java:324)
org.apache.commons.el.ArraySuffix.evaluate(ArraySuffix.java:314)
org.apache.commons.el.ComplexValue.evaluate(ComplexValue.java:145)
org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:263)
org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:190)
 
Hi,

The exception is thrown by the getInfo() method in Info class. Change the method getInfo() to handel the exception insted for throwing the exception. The above exception is thrown while creating the prepared statement.

Code:
public Collection getInfo(){
 ArrayList list = new ArrayList();
 InitialContext ctx = new InitialContext();
 //OTHER CONNECTION STATEMENTS
String sql = "SELECT ADDR FROM TABLENAME WHERE user= ?"; [COLOR=red] change from '?' to ? [/color]
  try {
       conn = ds.getConnection();
       pstmt = conn.prepareStatement(sql);
       pstmt.setString(1, this.user);
       rs = pstmt.executeQuery();
      while(rs.next()) {
        InfoBean tsel = new InfoBean(rs.getString("addr"), this.user);
       list.add(tsel);
      }
     }[COLOR=red]catch(NamingException ne){
       System.out.println(ne.toString());
     }catch(SQLException sqle){
       System.out.println(sqle.toString());
     }[/color]                    
  finally {
          //CLOSE pstmt, conn, and rs statements
  }
 return list;
}
 
well, now I dont get any errors but a blank page. I get the idea and will read more on Javabean...
thanks :)
 
Hi,

I think the ArrayList is empty, that the reason why the <c:forEach/> not iterating the the page is blank. Test the process by commenting the SQL call in getInfo() method;

Try some thing like this

Code:
public Collection getInfo(){
 ArrayList list = new ArrayList();
 [COLOR=red] /* [/color]InitialContext ctx = new InitialContext();
 //OTHER CONNECTION STATEMENTS
String sql = "SELECT ADDR FROM TABLENAME WHERE user= ?"; change from '?' to ?
  try {
       conn = ds.getConnection();
       pstmt = conn.prepareStatement(sql);
       pstmt.setString(1, this.user);
       rs = pstmt.executeQuery();
      while(rs.next()) {
        InfoBean tsel = new InfoBean(rs.getString("addr"), this.user);
       list.add(tsel);
      }
     }catch(NamingException ne){
       System.out.println(ne.toString());
     }catch(SQLException sqle){
       System.out.println(sqle.toString());
     }                    
  finally {
          //CLOSE pstmt, conn, and rs statements
  }[COLOR=red]*/[/color]
  for (int i=0; i<5; i++){
   list.add(new InfoBean(this.user+i, this.user+i));
  }
 return list;
}
 
I tried that. The Arraylist displayed both the addr and cust columns and data. I also tested the database and connection without the inserting it in an array and it displayed fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top