分配二维数组时出现Android NullPointerException错误

时间:2013-03-29 13:57:40

标签: arrays json nullpointerexception

我的网站有一个Json响应,我解析了这个响应。我可以写入屏幕这个解析数据没有问题,但我想要分配二维数组时出现NullPoniterException错误。

我定义了数组和jsonarray和jsonobject这样......

final String[][] sonuclarDizi = null;
    String kayitSayisi = jObj.getString("kayitSayisi");
 int kayitsay = Integer.parseInt(kayitSayisi.toString());
 JSONArray menuitemArray = jObj.getJSONArray("sonuclar");


for (int i = 0; i < kayitsay; i++) {
                                System.out.println(i+". sonucun sube id si "+menuitemArray.getJSONObject(i).getString("sube_id").toString());
                                System.out.println(i+". sonucun ders_id si "+menuitemArray.getJSONObject(i).getString("ders_id").toString());
                                System.out.println(i+". sonucun kodu si "+menuitemArray.getJSONObject(i).getString("kodu").toString());
                                System.out.println(i+". sonucun adi si "+menuitemArray.getJSONObject(i).getString("adi").toString());
                                System.out.println(i+". sonucun ortalama_yazi si "+menuitemArray.getJSONObject(i).getString("ortalama_yazi").toString());
                                System.out.println(i+". sonucun sube id si "+menuitemArray.getJSONObject(i).getString("kredi").toString());
                                System.out.println(i+". sonucun devamDurumu si "+menuitemArray.getJSONObject(i).getString("devamDurumu").toString());
                                System.out.println(i+". sonucun sube id si "+menuitemArray.getJSONObject(i).getString("ortalama").toString());
                                System.out.println(i+". sonucun sonuc si "+menuitemArray.getJSONObject(i).getString("sonuc").toString());
                                System.out.println(i+". sonucun sube id si "+menuitemArray.getJSONObject(i).getString("basarili").toString());
                                System.out.println(i+". sonucun harf si "+menuitemArray.getJSONObject(i).getString("harf").toString());
                            } 

这部分正在发挥作用。但是在下面部分返回NullPoniterException错误。

for (int i = 0; i < kayitsay; i++) {
                                sonuclarDizi[i][1]=(menuitemArray.getJSONObject(i).getString("sube_id").toString());
                                sonuclarDizi[i][1]=(menuitemArray.getJSONObject(i).getString("ders_id").toString());
                                sonuclarDizi[i][1]=(menuitemArray.getJSONObject(i).getString("kodu").toString());
                                sonuclarDizi[i][1]=(menuitemArray.getJSONObject(i).getString("adi").toString());
                                sonuclarDizi[i][1]=(menuitemArray.getJSONObject(i).getString("ortalama_yazi").toString());
                                sonuclarDizi[i][1]=(menuitemArray.getJSONObject(i).getString("kredi").toString());
                                sonuclarDizi[i][1]=(menuitemArray.getJSONObject(i).getString("devamDurumu").toString());
                                sonuclarDizi[i][1]=(menuitemArray.getJSONObject(i).getString("ortalama").toString());
                                sonuclarDizi[i][1]=(menuitemArray.getJSONObject(i).getString("sonuc").toString());
                                sonuclarDizi[i][1]=(menuitemArray.getJSONObject(i).getString("basarili").toString());
                                sonuclarDizi[i][1]=(menuitemArray.getJSONObject(i).getString("harf").toString());
                            }

非常多...

1 个答案:

答案 0 :(得分:0)

sonuclarDizi是一个空数组。你声明了它,但是你需要先实例化它才能添加项目。

String[][] sonuclarDizi;

sonuclarDizi = new String[kayitsay][];

for (int i = 0; i < kayitsay; i++) {
  sonuclarDizi[i] = new String[11];
  /* ... */
}