从文件Android创建数组时出现NullPointerException?

时间:2011-06-26 05:16:00

标签: android xml android-layout

当我从文件创建一个大数组时,我得到一个nullpointerexception。将为.txt文件的每一行创建一个新元素。当我使用从这个文件创建的数组时,我得到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;
}

1 个答案:

答案 0 :(得分:2)

首先我要提到大多数设备的最大堆大小为16MB,你可能会超过这个,但后来我找到了this page。似乎512是最大数组大小。我会尝试@kcoppock给出的建议,并尝试一个ArrayList。

作为旁注:似乎你正在将“单词”加载到数组中。市场上的设备没有足够快的速度可以以任何效率迭代172K项目阵列;我相信你正在为用户提供缓慢而痛苦的体验。