0

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;
    }
}
Eric Galluzzo
  • 3,066
  • 1
  • 17
  • 20
Jaberrio
  • 5
  • 3
  • Yes, I have now fixed that and used fileInputStream.read(readByte); instead of fileInputStream.read(); So now readByte has a nonNull Value. But I am still getting NullError When it should be returning "Random Text Goes Here" The Null Is now coming from fileInputStream.read(readByte); – Jaberrio Jun 20 '15 at 20:14
  • 1
    see this http://stackoverflow.com/questions/9095610/android-fileinputstream-read-txt-file-to-string – M4rtini Jun 20 '15 at 20:23

1 Answers1

0

The NPE is caused because readByte is null, which makes sense, because nowhere in the code you posted it is assigned a value.

This is probably what you are trying to accomplish:

fileInputStream = context.openFileInput(fileName);
readText = "";
byte[] buffer = new byte[1024];
while ((n = fileInputStream.read(buffer)) != -1) 
    readText += new String(buffer, 0, n);
wvdz
  • 15,266
  • 3
  • 43
  • 82
  • Okay When I Put fileInputStream.read(readByte); it should store what it read inside that buffer so now readByte has a value, however I am still geting NPE. – Jaberrio Jun 20 '15 at 20:08
  • Edit^ The Null is now coming from fileInputStream.read(readByte); – Jaberrio Jun 20 '15 at 20:16
  • I ended up using [this](http://stackoverflow.com/questions/9095610/android-fileinputstream-read-txt-file-to-string) however your solution works I just tested it. I will place your response as the answer as others might not want to implement the other. – Jaberrio Jun 20 '15 at 21:24