0

I have images in my assets folder which get called to my main activity when I select them in my recycler view activity.I also have a notification action to call back the main activity when needed (to access settings). However, when I call back the main activity by clicking on the notification action button, I get a nullpointer exception before the main activity shows. I have tried serializing my object so that it gets saved and therefore the nullpointer exception would go away, but it remains.

Here is my code:

        item = getIntent().getExtras().getParcelable(ITEM_KEY);
        try {
            ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(fileName));
            os.writeObject(item);
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (item == null) {
            pItem = getIntent().getExtras().getParcelable(CustomAdapter.ITEM_KEY_2);
            if (pItem == null) {
//                throw new AssertionError("Null data item received!");

                try {
                    ObjectInputStream is = new ObjectInputStream(new FileInputStream(fileName));
                    try {
                        Object object = is.readObject();
                        is.close();
                        if (object != null) {
                            object = item;

                        }
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

Here is the exception: "Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference"

It applies to "os.writeObject(item);"

I would be grateful for some pointers. Thanks!

Code Poet
  • 6,222
  • 2
  • 29
  • 50
  • 1
    Apparently `getIntent().getExtras()` returns `null` So you try to execute `null.getParcelable()`. Check for null before use. – greenapps Apr 11 '17 at 17:46
  • Thanks, I've just tried but the error persists. The strange thing is that it doesn't return null when I access my MainActivity from the RecyclerView. But it returns null when I try to access it from my notification. This is why I thought that serializing it would help save the object so that I can then go back to it. But it achieves nothing. – Code Poet Apr 11 '17 at 18:07

0 Answers0