【问题标题】:Reading file on Android throws a NullPointerException在 Android 上读取文件会引发 NullPointerException
【发布时间】:2015-09-06 14:56:16
【问题描述】:

尝试读取文件时出现以下错误(代码如下)。

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;
    }
}

【问题讨论】:

  • 是的,我现在已经解决了这个问题并使用了 fileInputStream.read(readByte);而不是 fileInputStream.read();所以现在 readByte 有一个非空值。但是我仍然收到 NullError 当它应该返回“Random Text Goes Here”时 Null 现在来自 fileInputStream.read(readByte);

标签: java android nullpointerexception


【解决方案1】:

NPE 是因为readByte 为空而引起的,这是有道理的,因为在您发布的代码中没有为它分配值。

这可能是你想要完成的:

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

【讨论】:

  • 好的,当我把 fileInputStream.read(readByte);它应该将读取的内容存储在该缓冲区中,所以现在 readByte 有一个值,但是我仍然得到 NPE。
  • Edit^ Null 现在来自 fileInputStream.read(readByte);
  • 我最终使用了this,但是您的解决方案有效,我刚刚对其进行了测试。我会将您的回复作为答案,因为其他人可能不想实施其他人。
猜你喜欢
  • 1970-01-01
  • 2013-04-11
  • 2012-10-21
  • 2012-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-27
  • 1970-01-01
相关资源
最近更新 更多