Java 嵌套数组中的NullPointerException

Java 嵌套数组中的NullPointerException,java,android,sqlite,nullpointerexception,Java,Android,Sqlite,Nullpointerexception,它在我的嵌套数组中。基本上我想在arrayIWantThisCat中检索 [10{1,2,3,4},20{5,6,7,8}] String[][]arrayIWantThisCat; 字符串[]arrayAddendAmounts; 试一试{ SQLiteAdapter info=新的SQLiteAdapter(此); info.open(); alAddends=info.getFinalCategorysTagAddend(); info.close(); //数组保存所有唯一的加数量 ar

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

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

String[][]arrayIWantThisCat;
字符串[]arrayAddendAmounts;
试一试{
SQLiteAdapter info=新的SQLiteAdapter(此);
info.open();
alAddends=info.getFinalCategorysTagAddend();
info.close();
//数组保存所有唯一的加数量
arrayAddendAmounts=alAddends.toArray(新字符串[alAddends.size()]);
}捕获(SQLITE){
Log.d(“标记”,“无法获取数据”,e);
返回;
}
int numberOfAddends=arrayAddendAmounts.length;
for(int i=0;i
您尚未在代码中初始化
arrayIWantThisCat
,但您正在通过索引引用它,在这一行:

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;
    }
}
这可能会导致您的
NullPointerException

您可能希望使用第一级大小对其进行初始化,例如:

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

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

您没有创建数组,只是创建了一个空引用。这会导致出现NullPointerException

arrayIWantThisCat = new String[myFirstLevelSize][];

String[]arrayIWantThisCat=新字符串[0][]


如果要修复它,请使用org.apache.commons.ArrayUtils来调整它的大小,或者改为使用一个集合/列表。

实际上,该集合/列表已“初始化”,只要
Alwhatcadouwant=info.getFinalCategoryScatidsBasedEnd(arrayAddendAmounts[i])初始化它。当然,谁知道……是的,这是真的。但是,它可以为null。只对整个堆栈跟踪注释了OP。它可能是
你想要的一切
(顺便说一句,我的眼睛在流血)。可能是什么。我一直假设——直到有相反的证据——代码的其余部分运行良好;作品虽然我的变量名是sudo名称,但您会对它们做哪些更改。“我总是愿意重新审视事物。”瑞克很高兴它奏效了。根据变量的范围,您的命名非常不清楚。我建议你看一下Java以获得一些提示,一般来说,环顾四周,你会发现“健谈”代码如何帮助可伸缩性等。这仍然意味着你在某种程度上使用了伪名称(是的,有点拖累你:D对不起)。这里有一些很重的变量/方法名称:p
info.getFinalCategorySpatidsBasedEnd(arrayAddendAmounts[i]);
让我的大脑有点扭曲为了清晰起见,我建议你发布整个堆栈跟踪。这样人们就会确切地知道
NullPointerException发生在哪里,以及可能的原因。对不起,我用//标记了它,这就是问题所在(嵌套数组)
arrayIWantThisCat = new String[myFirstLevelSize][];
String[][] arrayIWantThisCat;