I am getting the following error when attempting to read the file (code below).
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)
From what I understand, java.lang.NullPointerException
is telling me that when I call getDataBase()
, it is returning null
. However, how can this be? I call setDataBase()
first, which should create a file called DATA
and place "Random Text Goes Here".getBytes()
inside said file. So logically, when I read that same file it should return the bytes then decode the bytes using UTF-8. However this is not what is happening.
As a point of reference, line 37 is:
readText = new String(readByte, "UTF-8");
Thanks in advance.
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;
}
}