0
当我从文件创建大型数组时,我得到了一个nullpointerexception。将为.txt文件的每一行创建一个新元素。当我使用从这个文件创建的数组时,我得到了nullPointerException。从文件-Android创建数组时NullPointerException?
这里是我的代码:
static String[] results=new String[172820];
public String[] getWords(){
try{
InputStream fstream = getResources().openRawResource(R.raw.enable1);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
for (int x=0; (strLine = br.readLine()) != null; x++) {
results[x]=strLine;
}
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
return results;
}
您应该使用调试器的Step Over来定位哪一行代码会引发此异常。 –
或者在命令提示符下使用'adb logcat'。 – Haphazard
'static String [] results = new String [172820];'@。@不要这样做。使用ArrayList,您不必担心超出范围的异常,或者分配的数量超过您的需要。 – kcoppock