2017-04-14 13 views
0

データベースから配列c_pid[]にデータを入力しました。今私はループを実行しようとしている。配列の値がnullでない場合、ループは継続する必要があります。すべてが正常に動作するようです。私は希望の出力を得ています。しかし、私が持っている1つの問題は何らかの理由でそれが私にnullpointer例外を示しているということです。なぜ私はNullPointerExceptionを取得しているのかわかりません

私は以下のコードを提供しています。私はあなたのようなarray[index] == nullは、あなたがするwhile (!c_pid[cnt].equals(null)) {持っていますまで、問題がループする代わりに、ループの中で

<%@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); 
    }  
%> 
+0

nullポインタ例外を受け取ったときに、これも起こった行の情報を取得します。 – tilz0R

+1

あなたはこのようにnullを比較することはできません!c_pid [cnt] .equals(null)。これは(c_pid [cnt]!= null)のように比較してください。 –

+0

@ PorkkoMありがとう、それは働いた!!神様ありがとう –

答えて

0

java.lang.NullPointerExceptionエラーを取得イム

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

た後、このループを実行しようとしていますenter image description here

配列の最後までループしてください。

が代わりに使用するのでは

を提案する:

String[] c_pid = new String[100000]; 

あなたが代わりにリストを使用することができ、それが標準であり、あなたはそれがどのようなサイズのために開いている最大数でそれを初期化する必要はありません:

List<String> c_pid = new ArrayList<>(); 
... 
c_pid.add(dt.rs.getString("user_prod_id")); 
... 
out.println("Size of array is " + c_pid.size() + "<br />"); 
.... 
out.println("Size of array is " + c_pid.size() + "<br />"); 
for (String str : c_pid) { 
    out.println(str + "<br />"); 
} 
0

while (!c_pid[cnt].equals(null))ここは悪です。これをwhile(c_pid[cnt]!=null)に置き換え、目的を解決する必要があります。

関連する問題