I am getting a nullpointerexception when I create a large array from a file. A new element is to be created for each line of the .txt file. I get the nullPointerException when I use the array created from this file.
Here is my code:
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;
}
At first I was going to mention that most devices have a maximum heap size of 16MB and that you might be exceeding that, but then I found this page. It would appear that 512 is the maximum array size. I would try the suggestion given by @kcoppock, and try an ArrayList.
As a side note: it would seem you’re loading “words” into an array. No device on the market is anywhere near fast enough to iterate over a 172K item array with any efficiency; I believe you’re setting the user up for a slow and painful experience.