2

Edit for clarification: Why this does not appear to be a normal null reference is the fact that I have already used this code in another form and have had no issue there. I could manually delete the file from my device and the file will just be replaced later. This should be handled by my try/catch statement where if the file does not exist it will initialize a new object for me.

Furthermore when I get the error in android studio upon running it, the error points directly at the FileInputStream fis = openFileInput(FILENAME);

Another thing to note is that when I add an item to the object that I am trying to pass into gson I have no issues. The object is created and I have no issue whatsoever adding items to it.

Furthermore as context, I have these two functions in an object class that manages my data for me. I create a single one of these objects as a DataManager and use these functions to save/load (among others) an object that contains all of my data within itself.

When I was putting the class together I had to add that the object class extends Activity

Because I was getting the fileoutput and fileinput as red functions beforehand. However this seems very sketchy to me (and perhaps explains exactly why I am seeing this error...?)

Original before edit: In my Android Studio application I've been trying my best to utilize gson and at present I am attempting to save a single object containing multiple attributes using gson.

However I just successfully managed to use this same gson code to put an ArrayList into a file. I could delete that file myself and everything would work just fine. However I am now trying to place an entire object in.

The error is: "java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)' on a null object reference"

The code looks like the following:

public void loadFromFile() {
    try {
        FileInputStream fis = openFileInput(FILENAME);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));
        Gson gson = new Gson();
        // https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html, 2015-09-23
        Type objectType = new TypeToken<StatisticsRecordObject>() {}.getType();
        recordObject = gson.fromJson(in, objectType);

    } catch (FileNotFoundException e) {
        recordObject = new StatisticsRecordObject();
    }
}


public void saveInFile() {
    try {
        FileOutputStream fos = openFileOutput(FILENAME, 0);

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos));

        Gson gson = new Gson();


        gson.toJson(recordObject, out);
        out.flush(); //same as fflush as before. Buffer must go. FLUSH AFTER WRITING.
        fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        recordObject = new StatisticsRecordObject();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        throw new RuntimeException(e);
    }
}
NeuralCode
  • 135
  • 1
  • 2
  • 13
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Andreas Oct 02 '15 at 04:50

1 Answers1

0

I think the NullpointerException says that context object that you call "openFileForOutput" is null

http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String, int)

You should probably pass in a context to the method and make sure that the context is proper initialised.

seba.wagner
  • 3,800
  • 4
  • 28
  • 52