简体   繁体   English

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

[英]to compare values of jsp page with the database values null pointer exception

i want to compare the strings that are taken from form with the values with the table values... but there is nullpointerexception 我想比较从表单获取的字符串与值与表值...但有nullpointerexception
2)how do i get resultset to not access the last record of the table 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);

            } 

the database values are - 数据库值是 -

OccId FirstName LastName Village SonOf District OccId FirstName LastName Village SonOf区

31 Sourodeep bag bengal aurobindo bad bengal 31 Sourodeep袋孟加拉aurobindo坏孟加拉

32 ARVINDBHAI PARMAR SAJOD PARAGBHAI SAJOD 32 ARVINDBHAI PARMAR SAJOD PARAGBHAI SAJOD

67 divyang parmar bharuch Arvindbhai bharuch 67 divyang parmar bharuch Arvindbhai bharuch

72 urvashi parmar mumbai arvindbhai parmar mumbai 72 urvashi parmar孟买arvindbhai parmar孟买

90 divyang NULL NULL NULL NULL 90 divyang NULL NULL NULL NULL

91 divyang NULL NULL NULL NULL 91 divyang NULL NULL NULL NULL

  1. You have to Check for null 你必须检查null

    if(first!=null) 如果(第一!= NULL)

2.If it is a null stirng, then null not equals NULL because you have used equals , use equalsIgnoreCase Instead. 2.如果它是一个空的搅拌,那么null不等于NULL因为你已经使用了equals,而是使用equalsIgnoreCase。

The docs for ResultSet#getString say: ResultSet#getString的文档说:

the column value; 列值; if the value is SQL NULL, the value returned is null 如果值为SQL NULL,则返回的值为null

So last is null for those rows for example, not the String "null" . 因此,对于那些行, lastnull ,而不是String "null" You are therefore calling null.equals . 因此,您调用null.equals

Test for == null , not equals("null") : 测试== 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) )

check your 检查你的

if(first!=null) 如果(第一!= NULL)

then compare the values with other 然后将这些值与其他值进行比较

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM