0

I have inputted data into an array c_pid[] from the database. Now i'm trying to run a loop. If the the value an array is not null then the loop should carry on. Everything seems to run fine. I am getting the desired output. But one problem which i have is that for some reason it is showning me nullpointer exception.

I have provided the code as well as the screenshot down below.enter image description here

I am trying to run this loop

while(!c_pid[cnt].equals(null)){

after which im getting java.lang.NullPointerException error

<%@page import="storage.data"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    String[] c_pid = new String[100000];
    String c = "", pna = "", pty = "", ppr = "", stock = "", imgpath = "";
    //String myList = new String[10];
    int a = 0, d = 0;
    int result = 0, count = 0;
    int setres;
    int[] arr = new int[100000];
    String testval = "75";
    int item_id = 0;
    data dt = new data();
    String item = "", cartid = "", user = "";
    String[] prdid = new String[60];
    int cnt = 0;

    try {
        dt.st = dt.cn.createStatement();
        String select_match = "SELECT user_prod_id, COUNT(*) AS rep "
                + "FROM cart_table "
                + "GROUP BY user_prod_id "
                + "ORDER BY rep desc";
        dt.rs = dt.st.executeQuery(select_match);

        while (dt.rs.next()) {
            //prdid[a] = dt.rs.getString("user_prod_id");
            //a=a+1;
            c_pid[cnt] = dt.rs.getString("user_prod_id");
            cnt = cnt + 1;
        }
        out.println("<br/>---------xxx--------");
        String select3 = "select "
                + "product_table.p_id,product_table.p_type,"
                + "product_type.pt_id,"
                + "product_table.p_name,product_table.imgpath,product_table.p_price,product_table.stock,product_table.add_date,"
                + "product_type.pt_name "
                + "from product_table "
                + "inner join product_type "
                + "on product_table.p_type=product_type.pt_id "
                + "order by product_table.add_date desc"
                + "";
        cnt = 0;
        int size = c_pid.length;
        out.println("Size of array is " + size + "<br />");
        while (!c_pid[cnt].equals(null)) {
            out.println(c_pid[cnt] + "<br />");

            cnt = cnt + 1;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        out.println(ex);
    }       
%>
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
MUSTAFA BURHANI
  • 53
  • 1
  • 1
  • 3

1 Answers1

0

while (!c_pid[cnt].equals(null)) this is a evil here. Replace this with while(c_pid[cnt]!=null) and it should solve the purpose.

ProgrammerBoy
  • 876
  • 6
  • 19