比较jsp页面的值与数据库值空指针异常

时间:2013-03-14 17:51:57

标签: java database jsp servlets

我想比较从表格中获取的字符串与带有表值的值...但是有nullpointerexception
2)如何让结果集不访问表的最后一条记录

        String occid = request.getParameter("occid");
        String date = request.getParameter("Date");
        String firstname = request.getParameter("FirstName");
        String lastname = request.getParameter("LastName");
        String village = request.getParameter("Village");
        String sonof = request.getParameter("Son");
        String district = request.getParameter("District");
        String weight = request.getParameter("Weight");
        String bags = request.getParameter("Bags");
        String rate = request.getParameter("Rate");
        String amount = request.getParameter("Amount");
        String advanceamt = request.getParameter("Advance");

        String sql = null;
        PreparedStatement prest = null;
        List<AcceptBean> list = new ArrayList<AcceptBean>();

        sql = "Select * from ColdStorage.OccupantMaster";
        prest = conn.prepareStatement(sql);
        ResultSet rs = prest.executeQuery();
        String first = "";
        String last = "";
        String vill = "";
        String son = "";
        String dist = "";
        while (rs.next()) {
            AcceptBean bean = new AcceptBean();
            first = rs.getString("FirstName");
            bean.setFirstName(first);
            System.out.println("first = " + first);
            last = rs.getString("LastName");
            bean.setLastName(last);
            System.out.println("last = " + last);
            vill = rs.getString("Village");
            bean.setVillage(vill);
            System.out.println("vill = " + vill);
            son = rs.getString("Sonof");
            bean.setSonOf(son);
            System.out.println("son = " + son);
            dist = rs.getString("District");
            bean.setDistrict(dist);
            System.out.println("dist = " + dist);
            list.add(bean);
        }
        for (AcceptBean s : list) {
            if ((first.equals(firstname) || first.equals("null")) && (last.equals(lastname) || last.equals("null")) &&
                    (vill.equals(village) || vill.equals("null")) && (son.equals(sonof) || son.equals("null")) &&
                    (dist.equals(district) || dist.equals("null"))) 

            {
                System.out.println("do nothing");
                ServletContext sc = getServletContext();
                RequestDispatcher rd = sc.getRequestDispatcher("/Index.jsp");
                System.out.println("it exists in database");
                rd.forward(request, response);

            } 

数据库值为 -

OccId 名字 姓 村 的儿子 区

31 Sourodeep 袋 孟加拉 aurobindo不好 孟加拉

32 ARVINDBHAI 帕尔马 SAJOD PARAGBHAI SAJOD

67 divyang 帕尔马 巴鲁奇 Arvindbhai 巴鲁奇

72 瓦什 帕尔马 孟买 arvindbhai parmar 孟买

90 divyang 空值 空值 空值 NULL

91 divyang 空值 空值 空值 NULL

3 个答案:

答案 0 :(得分:1)

  1. 您必须检查空

    如果(第一!= NULL)

  2. 2.如果它是一个空的搅拌,那么null不等于NULL,因为你已经使用了等号,而是使用equalsIgnoreCase。

答案 1 :(得分:1)

ResultSet#getString的文档说:

  

列值;如果值为SQL NULL,则返回的值为null

因此,对于这些行,lastnull,而不是String "null"。因此,您正在呼叫null.equals

测试== null,而不是equals("null")

        if( first == null || first.equals(firstname) &&
            last == null || last.equals(lastname) &&
            vill == null || vill.equals(village) &&
            son == null || son.equals(sonof) &&
            dist == null || dist.equals(district) )

答案 2 :(得分:0)

检查你的

if(first!= null)

然后将这些值与其他

进行比较