爲什麼我得到一個NullPointerException這個執行準備語句的代碼?爲什麼我得到一個NullPointerException與執行預準備語句的代碼?
public ArrayList<BeanStaff> getLeaveStatus(ArrayList<String> name,String date) throws Exception
{
ArrayList<BeanStaff> lDetail = new ArrayList();
String r = name.toString();
Statement st = null;
DateFormat formatter = null;
Date convertedDate = null;
formatter = new SimpleDateFormat("dd/MM/yyyy");
convertedDate = (Date) formatter.parse(date);
java.sql.Date dt = new java.sql.Date(convertedDate.getTime());
try {
PreparedStatement ps = con.prepareStatement("Select Leave_Status from staff_leave_detail join employe on staff_leave_detail.Emp_ID = employe.Emp_ID where Leave_Start_Date=? and Emp_F_Name=?");
ps.setDate(1,dt);
ps.setString(2,r);
ps.addBatch();
if(r.equals(name.size()))
{
ps.executeBatch();
}
while(ps.getMoreResults())
{
rs = ps.getResultSet();
}
while(rs.next())
{
BeanStaff sf = new BeanStaff();
sf.setLeaveStatus(rs.getString("Leave_Status"));
lDetail.add(sf);
}
}
catch (Exception e)
{
System.out.println(e);
}
finally
{
con.close();
st.close();
}
return lDetail;
}
'但得到空指針異常' - 我們不介意讀者。我們不知道哪個語句導致異常。閱讀您的堆棧跟蹤以找出存在問題的語句。然後找到空的變量。然後修復它,使變量不爲空。 – camickr
camickr:我在ps.getResultSet()中獲得空值.. – user2495933
user2495933,如果你堅持一致的代碼格式化風格,你會讓自己和你的同事們更快樂。 –