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