4

所以我试图从android中的文件中读取。我初始化了所有东西,但我仍然得到一个NullPointerException。我错过了什么吗?

在第 25 行收到错误

public class Read {
    private ArrayList<String> contents;
    private final String filename = "saves/user.txt";

    public Read(Context context) {
        try {
            contents = new ArrayList<String>();
            InputStream in = context.getAssets().open(filename);

            if (in != null) {
                // prepare the file for reading
                InputStreamReader input = new InputStreamReader(in);
                BufferedReader br = new BufferedReader(input);
                String line = br.readLine();
                while (line != null) {
                    contents.add(line);
                }
                in.close();
            }else{
                System.out.println("It's the assests");
            }

        } catch (IOException e) {
            System.out.println("Couldn't Read File Correctly");
        }
    }

    public ArrayList<String> loadFile() {
        return this.contents;
    }
}
4

1 回答 1

3

这样做

 while ((line=reader.readLine()) != null) 
 {
    contents.add(line);
 }
于 2013-03-08T03:32:20.877 回答