为什么我使用正在执行预准备语句的代码获取NullPointerException?

时间:2013-08-19 05:00:15

标签: java mysql

为什么我使用正在执行预准备语句的代码获取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;
}

1 个答案:

答案 0 :(得分:1)

你有这个条件:

if(r.equals(name.size()))

由于您之前将r初始化为:

,因此这似乎有问题
String r = name.toString();

ArrayList的大小如何等于相同的ArrayList&#39; s toString()

我相信这种情况总是失败(返回false)而你的ps.executeBatch()永远不会被调用。