java - Dont know why I am getting a NullPointerException -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i have inputted data array c_pid[]
database. i'm trying run loop. if the value array not null loop should carry on. seems run fine. getting desired output. 1 problem have reason showning me nullpointer exception.
i have provided code screenshot down below.enter image description here
i trying run loop
while(!c_pid[cnt].equals(null)){
after 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 = 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(*) rep " + "from cart_table " + "group user_prod_id " + "order 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 product_table.add_date desc" + ""; cnt = 0; int size = c_pid.length; out.println("size of array " + 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); } %>
the problem in loop instead loop until array[index] == null
while (!c_pid[cnt].equals(null)) {
have loop until end of array.
suggest
instead of using :
string[] c_pid = new string[100000];
you can use list instead, standard , don't need initialize max number open size :
list<string> c_pid = new arraylist<>(); ... c_pid.add(dt.rs.getstring("user_prod_id")); ... out.println("size of array " + c_pid.size() + "<br />"); .... out.println("size of array " + c_pid.size() + "<br />"); (string str : c_pid) { out.println(str + "<br />"); }
Comments
Post a Comment