getString servlet中的空指针异常

时间:2014-04-11 08:20:19

标签: java servlets jdbc

我有web的servlet:

String value = request.getParameter("valuechannel");
    String type = request.getParameter("type");
    String teller = request.getParameter("teller");
    PrintWriter out = response.getWriter();
    proses_dbase_user pdb = new proses_dbase_user();
    ArrayList<mo_tell_suc_err> tabtel = new ArrayList<mo_tell_suc_err>();


    try
    {
        tabtel = pdb.disTelSucc(new mo_tell_suc_err(value,type,teller));
        out.print(tabtel);

        JSONObject jso = new JSONObject();

        jso.put("items", tabtel);

        Gson gson = new Gson();
        String json = gson.toJson(jso);
        out.println(json);
    }
    catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

用于过程数据库

public ArrayList disTelSucc(mo_tell_suc_err mor)throws SQLException
{
    getConnection();
    //pstmt = koneksi.prepareStatement(query); 
    //rs = pstmt.executeQuery();
    if(mor.getType().toLowerCase().equals("financial"))
    {
        query = "SELECT trans_code, trans_desc, total_success FROM tb_vw_teller_succ_err_fin WHERE branch=? AND teller=?";

        pstmt.setString(1, mor.getBranch());
        pstmt.setString(2, mor.getTeller());
        pstmt = koneksi.prepareStatement(query);
        rs = pstmt.executeQuery();
    }
    else
    {
        query = "SELECT trans_code, trans_desc, total_success FROM tb_vw_teller_succ_err_non_fin WHERE branch=? AND teller==?";

        pstmt.setString(1, mor.getBranch());
        pstmt.setString(2, mor.getTeller());
        pstmt = koneksi.prepareStatement(query);
        rs = pstmt.executeQuery();  
    }


    while(rs.next())
    {
        mor.setTRX_CODE(rs.getString(1));
        mor.setTRX_DESC(rs.getString(2));
        mor.setTOTAL_SUCCESS(rs.getString(3));

        teller_succ_err.add(mor);
    }
    return teller_succ_err;
}

和模型

public class mo_tell_suc_err {


private String tempTypeTeller;
private String tempNonFin;
private String tempType;
private String TRX_CODE;
private String TRX_DESC;
private String ERROR_CODE;
private String ERROR_DESC;
private String TOTAL_SUCCESS;
private String TOTAL_ERROR;

public mo_tell_suc_err()
{

}
public mo_tell_suc_err(String tempType,String tempNonFin, String tempTypeTeller)
{
    this.tempType = tempType;
    this.tempNonFin = tempNonFin;
    this.tempTypeTeller = tempTypeTeller;
}
public String getTeller() {
    return tempTypeTeller;
}
public void setTeller(String tempTypeTeller) {
    this.tempTypeTeller = tempTypeTeller;
}
public String getType() {
    return tempNonFin;
}
public void setType(String tempNonFin) {
    this.tempNonFin = tempNonFin;
}
public String getBranch() {
    return tempType;
}
public void setBranch(String tempType) {
    this.tempType = tempType;
}
public String getTRX_CODE() {
    return TRX_CODE;
}
public void setTRX_CODE(String TRX_CODE) {
    this.TRX_CODE = TRX_CODE;
}
public String getTRX_DESC() {
    return TRX_DESC;
}
public void setTRX_DESC(String TRX_DESC) {
    this.TRX_DESC = TRX_DESC;
}
public String getERROR_CODE() {
    return ERROR_CODE;
}
public void setERROR_CODE(String ERROR_CODE) {
    this.ERROR_CODE = ERROR_CODE;
}
public String getERROR_DESC() {
    return ERROR_DESC;
}
public void setERROR_DESC(String ERROR_DESC) {
    this.ERROR_DESC = ERROR_DESC;
}
public String getTOTAL_SUCCESS() {
    return TOTAL_SUCCESS;
}
public void setTOTAL_SUCCESS(String TOTAL_SUCCESS) {
    this.TOTAL_SUCCESS = TOTAL_SUCCESS;
}
public String getTOTAL_ERROR() {
    return TOTAL_ERROR;
}
public void setTOTAL_ERROR(String TOTAL_ERROR) {
    this.TOTAL_ERROR = TOTAL_ERROR;
}

}

但我在我的代码中得到以下异常:

我经常遇到这个错误。

java.lang.NullPointerException
com.bni.dbase.proses_dbase_user.disTelSucc(proses_dbase_user.java:661)
com.bni.user.proses_teller_success.doGet(proses_teller_success.java:51)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

请帮助我知道异常所指的位置,因为我试图改变模型和流程数据库仍然没有发生

谢谢

1 个答案:

答案 0 :(得分:0)

您在创建pstmt.setString(1, mor.getBranch())之前呼叫pstmt。代码应如下:

pstmt = koneksi.prepareStatement(query);
pstmt.setString(1, mor.getBranch());
pstmt.setString(2, mor.getTeller());