【发布时间】: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