嵌套数组中的NullPointerException

时间:2013-08-08 18:53:31

标签: java android sqlite nullpointerexception

java.lang.NullPointerException

它在我的嵌套数组中。基本上我想在arrayIWantThisCat

中检索
  

[10 {1,2,3,4},20 {5,6,7,8}]

String[][] arrayIWantThisCat;
String[] arrayAddendAmounts;

try {
    SQLiteAdapter info = new SQLiteAdapter(this);
    info.open();
    alAddends = info.getFinalCategorysTagAddend();
    info.close();
    //  Array holds all unique Addend amounts
    arrayAddendAmounts = alAddends.toArray(new String[alAddends.size()]);               
 } catch (SQLiteConstraintException e) {
    Log.d("TAG", "failure to get data,", e);
    return;
 }

 int numberOfAddends = arrayAddendAmounts.length;
  for (int i = 0; i < numberOfAddends; i++) {
try {
    SQLiteAdapter info = new SQLiteAdapter(this);
    info.open();
    alWhatCatDoYouWant = info.getFinalCategorysCatIDsBasedOnAddend(arrayAddendAmounts[i]);
    info.close();

         ***//  This is where the issue is ( Nested Array )***
    arrayIWantThisCat[i] = alWhatCatDoYouWant.toArray(new String[alWhatCatDoYouWant.size()]);               
    } catch (SQLiteConstraintException e) {
    Log.d("TAG", "failure to get data,", e);
    return;
    }
}

3 个答案:

答案 0 :(得分:0)

您尚未在代码中初始化arrayIWantThisCat,但您在此行中按索引引用它:

arrayIWantThisCat[i] = alWhatCatDoYouWant.toArray(new String[alWhatCatDoYouWant.size()]);

这可能会导致您NullPointerException

您可能希望使用第一级大小初始化它,例如:

arrayIWantThisCat = new String[myFirstLevelSize][];

...在访问其索引i之前,该索引可以等于myFirstLevelSize - 1

答案 1 :(得分:0)

您尚未创建数组,只是一个null的引用。这导致NullPointerException发生。

String[][] arrayIWantThisCat;

答案 2 :(得分:0)

String[][] arrayIWantThisCat = new String[0][];

应该修复它,使用org.apache.commons.ArrayUtils来调整它或者改为设置/列表。