在Android上读取文件会抛出NullPointerException

时间:2015-06-20 19:50:22

标签: java android nullpointerexception

尝试读取文件时,我收到以下错误(代码如下)。

06-20 14:28:08.742    3156-3156/org.jaberrio.personai2 W/System.err﹕ java.lang.NullPointerException
06-20 14:28:08.742    3156-3156/org.jaberrio.personai2 W/System.err﹕ at java.lang.String.<init>(String.java:228)
06-20 14:28:08.742    3156-3156/org.jaberrio.personai2 W/System.err﹕ at org.jaberrio.personai2.DataBaseManager.getDataBase(DataBaseManager.java:37)
06-20 14:28:08.742    3156-3156/org.jaberrio.personai2 W/System.err﹕ at org.jaberrio.personai2.OverviewLand.onClick(OverviewLand.java:93)
06-20 14:28:08.742    3156-3156/org.jaberrio.personai2 W/System.err﹕ at android.view.View.performClick(View.java:4438)
06-20 14:28:08.742    3156-3156/org.jaberrio.personai2 W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422)
06-20 14:28:08.742    3156-3156/org.jaberrio.personai2 W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
06-20 14:28:08.742    3156-3156/org.jaberrio.personai2 W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
06-20 14:28:08.742    3156-3156/org.jaberrio.personai2 W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
06-20 14:28:08.742    3156-3156/org.jaberrio.personai2 W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5001)
06-20 14:28:08.742    3156-3156/org.jaberrio.personai2 W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
06-20 14:28:08.742    3156-3156/org.jaberrio.personai2 W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
06-20 14:28:08.742    3156-3156/org.jaberrio.personai2 W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-20 14:28:08.742    3156-3156/org.jaberrio.personai2 W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-20 14:28:08.742    3156-3156/org.jaberrio.personai2 W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

根据我的理解,java.lang.NullPointerException告诉我,当我致电getDataBase()时,它正在返回null。但是,这怎么可能呢?我先调用setDataBase(),这应创建一个名为DATA的文件,并将"Random Text Goes Here".getBytes()放在所述文件中。所以逻辑上,当我读取相同的文件时,它应该返回字节,然后使用UTF-8解码字节。然而,这不是正在发生的事情。

作为参考,第37行是:

readText = new String(readByte, "UTF-8");

提前致谢。

package org.jaberrio.personai2;


import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class DataBaseManager {

    String fileName = "DATA";
    String inputText = "Random Text Goes Here";
    FileOutputStream fileOutputStream;
    FileInputStream fileInputStream;
    String readText = null;
    byte[] readByte = null;

    public void setDataBase(Context context) {

        try {
            fileOutputStream = context.openFileOutput(fileName, context.MODE_PRIVATE);
            fileOutputStream.write(inputText.getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getDataBase(Context context){
        try {
            fileInputStream = context.openFileInput(fileName);
            fileInputStream.read();
            readText = new String(readByte, "UTF-8");
            fileInputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return readText;
    }
}

1 个答案:

答案 0 :(得分:0)

导致NPE是因为readByte为空,这是有道理的,因为在您发布的代码中没有任何地方被分配了值。

这可能是你想要完成的事情:

fileInputStream = context.openFileInput(fileName);
readText = "";
byte[] buffer = new byte[1024];
while ((n = fileInputStream.read(buffer)) != -1) 
    readText += new String(buffer, 0, n);