我正在使用以下內容將檢索到的值添加到類中。所有的值將被添加到類的屬性,但我正在使用compisition(在類中有一個類的對象),它不顯示任何輸出。如何將結果集複製到對象中?
class employee
{
....
private Address address = new Address();
.....
}
...
Employee emp = new Employee();
try {
ps = con.prepareStatement("select * from employee,address "
+ "WHERE employee.username = ? AND "
+ "employee.ADD_ID = address.ID");
ps.setString(1, username);
ResultSet r = ps.executeQuery();
if (r.next()) {
BeanProcessor bp = new BeanProcessor();
emp = bp.toBean(r,Employee.class);
System.out.println("blockkkk:"+emp.getAddress().getBlock());
//output of above line is blockkkk:null
}
con.close();
ps.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
return emp;
Address類是如下:
public class Address {
.....
private String block;
....
public String getBlock() {
return block;
}
public void setBlock(String block) {
this.block = block;
}
....
}
NPE指向哪裏? – 2013-02-18 05:07:44
很有可能當你的BeanProcessor創建** emp **對象時,它不會通過在你的Employee對象中爲它創建一個新的實例來初始化你的Address對象。請檢查 – SudoRahul 2013-02-18 05:07:46