我正在尝试使用 BeanProcessor 填充我的类,但它部分有效。我的课程如下
地址类
public class Address {
..All attributes of address class go here...
.. All setter and getters go here...
}
将地址类作为其成员的员工类
public class Employee {
private int ID;
private String fname;
private String lname;
private String mobile;
private String phone;
private String email;
private String position;
private String title;
private String username;
private String password;
private String question;
private String answer;
private Address address; << address class is a member of employee class
.. All setter and getters go here....
}
我的模型如下:
Employee emp = new Employee();
try {
ps = con.prepareStatement("select * from employee,address "
+ "WHERE employee.username = ? AND "
+ "employee.AddID = 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("name:" + emp.getName()); << shows the name correctly
System.out.println("block:"+emp.getAddress().getBlock());<< the output is null
}
con.close();
ps.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
return emp;
}
当我运行应用程序时,它显示 emp 对象已正确填充,但其中的 Address 对象未正确填充,并且它为 getBlock() 方法返回 null,我检查了数据库块是否有值。根据spiritwalker的回答,因为它没有抛出nullpointer异常,所以转换是正确的,如果那是错误的,你有什么建议作为BeanProcessor的替代品?