我开发了jsf应用程序。我有一个MySQL Student表,包含30-40列和600行。以下是StudentBean中的代码:
@ManagedBean(name = "student")
@SessionScoped
public class StudentBean implements Serializable{
private List<Student> studentlist = new ArrayList<Student>();
private Student student = new Student();
private HtmlDataTable datatable;
private HtmlInputHidden studentnpm = new HtmlInputHidden();
private String querybynpm;
private String querybyangkatan;
private String status;
private String result;
public String init() {
try {
Class.forName(JurusanConection.getDriver());
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
Connection con = DriverManager.getConnection(JurusanConection.getUrl(),
JurusanConection.getUser_name(), JurusanConection.getPass_word());
String query = "SELECT * FROM student";
Statement ps = con.createStatement();
ResultSet rs = ps.executeQuery(query);
while (rs.next()) {
Student students = new Student();
students.setNpm(rs.getString("npm"));
students.setNama(rs.getString("nama").toString());
//setter and getter
students.setTanggalregistrasi(rs.getString("tanggalregistrasi").toString());
students.setDosenwali(rs.getString("dosenwali").toString());
students.setEmail(rs.getString("email"));
students.setKelas(rs.getString("kelas"));
studentlist.add(students);
}
result="mahasiswa";
ps.close();
con.close();
} catch (SQLException e) {
setResult(null);
setStatus(e.getMessage());
}
return result;
}
我没有通过构造函数填充数据库,而是调用init()方法。我收到一条错误消息javax.faces.el.EvaluationException:java.lang.NullPointerException。但是当我修改查询以限制显示150行的数字时,没有错误。任何人都可以弄清楚如何显示这600行没有错误。谢谢......
答案 0 :(得分:1)
您可能会在以下不安全的代码行中获取NullPointerExceptions:
rs.getString("nama").toString()
rs.getString("tanggalregistrasi").toString()
rs.getString("dosenwali").toString()
这假设从不是记录集中这三列的空值。您应该将此分配给变量并在使用.
运算符之前进行空检查。
此外,你为什么在这里调用toString()? ResultSet.getString
方法已经返回一个String对象。这完全没必要。