Prepared Statement似乎在java servlet中始终返回null

时间:2012-12-09 15:36:28

标签: java mysql nullpointerexception prepared-statement

我正在开发一个项目,用户应该在表单中填写他想要的标准,然后根据这些标准,servlet从数据库返回相应的结果;我正在使用mysql服务器。 我的问题是我用过的Prepared Statement(因为用户可以用可变数量的标准搜索)总是返回null,所以我得到了一个

Exception: null java.lang.NullPointerException at    
Servlet_GrowerResults.doGet(Servlet_GrowerResults.java:102)

这是getData类中的方法fetchGrowers,它将ResultSet返回给Servlet_GrowerResults

private PreparedStatement stmt_growers=null;
private ResultSet rs_growers;
private final String getGrowersQuery="select * from grower where name like ? and surname like ? and area like ? and partnership like ?;";

public ResultSet fetchGrowers(String criteria_name,String criteria_surname,String criteria_area, String criteria_partnership){
    try{
        stmt_growers=dbcon.getConnection().prepareStatement(getGrowersQuery);
        stmt_growers.setString(1, criteria_name);
        stmt_growers.setString(3, criteria_surname);
        stmt_growers.setString(5, criteria_area);
        stmt_growers.setString(7, criteria_partnership);

        rs_growers=stmt_growers.executeQuery();
        return rs_growers;
    }
    catch(Exception e9){
        errorMessage = "Error while getting all growers from database!<br>" + e9.getMessage();
        return null;
    }

这是初始化ResultSet的行。如果要涵盖所有提交标准的案例,还有其他其他方法

   try{
        getData.getDBcon().open();
        ResultSet rsAreas=getData.fetchGrowerAreas();
        ResultSet rsPartnership=getData.fetchGrowerPartnership();
        ResultSet rsGrowersReturned;

        String sqlWildcard="%";

        //Cases of criteria submited by the user,
        //in order to send the apropriate parameters
        //to the PreparedStatement

        //1st case: searching without names criteria
        if( (criteriaNames.length()>0) && (!(criteriaAreas.length()>0)) && (!(criteriaPartnerships.length()>0)) ){
            rsGrowersReturned=getData.fetchGrowers(sqlWildcard,sqlWildcard,criteriaAreas,criteriaPartnerships);
        }

这里是Exception指向的行。更具体地说,第102行是:while(rsGrowersReturned.next())

 if((sorting.equals("περιοχή"))){
        while(rsAreas.next()){
                out.println("<table>");
                String area=rsAreas.getString("area");
                area = new String(area.getBytes("ISO-8859-1"), "ISO-8859-7");
                out.println("<tr><th>"+rsAreas.getString("area")+"</th></tr>");

    /*line 102*/ while(rsGrowersReturned.next()){
                    if(rsGrowersReturned.getString("area").equals(rsAreas.getString("area"))){
                        String name=rsGrowersReturned.getString("name");
                        name = new String(name.getBytes("ISO-8859-1"), "ISO-8859-7");
                        String surname=rsGrowersReturned.getString("surname");
                        surname = new String(surname.getBytes("ISO-8859-1"), "ISO-8859-7");
                        String partnership=rsGrowersReturned.getString("partnership");
                        partnership = new String(partnership.getBytes("ISO-8859-1"), "ISO-8859-7");

                        out.println("<tr><td>"+name+" "+surname+"</td>");
                        out.println("<td>"+partnership+"</td></tr>");
                    }//end of neaced if
                }//end of second while
            }//end of first while
            out.println("</table>");
        }//end of if

我可以看到确实存在这个问题,因为在它到达第二个区域之前它打印了第一个区域,因为它应该做,然后Exception!

我尝试将sql查询直接发送到mysql服务器并且工作正常,我只是找不到问题!每个答案都将不胜感激,谢谢。

3 个答案:

答案 0 :(得分:3)

您需要按顺序设置PrepareStatement的输入参数:

stmt_growers.setString(1, criteria_name);
stmt_growers.setString(2, criteria_surname);
stmt_growers.setString(3, criteria_area);
stmt_growers.setString(4, criteria_partnership);

答案 1 :(得分:1)

我打赌这里吞下了一个SQLException:

catch(Exception e9){
    errorMessage = "Error while getting all growers from database!<br>" + e9.getMessage();
    return null;
}

修复此问题,以记录发生的异常或重新抛出适当的更高级别异常。在错误时静默返回null 总是是一个坏主意。

当您添加发布的帖子时,我们可以尝试帮助您跟踪原因。

答案 2 :(得分:0)

fetchGrowers有一个次优的错误处理。您只是捕获异常,创建一个未使用的消息并返回null,这将导致以后的nullpointer,我们不知道为什么。你应该至少记录一些东西,更好地(重新)抛出异常。