1

I'm trying to send two custom objects, one that stores an instance of another, along with an intent to another activity. Both of these implement Serializable, like so:

public class Story implements Serializable {

    private String title, author;
    private ArrayList<StorySegment> segments;

... }


class StorySegment implements Serializable {

    private String author;
    private String contents;

... }

And in my StoryOverviewAdapter class, I've got this:

public class StoryOverviewAdapter extends ArrayAdapter<Story> {

    public StoryOverviewAdapter(Context ctx, ArrayList<Story> stories) {
        super(ctx, 0, stories);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.story_overview_item, parent, false);
        }

        final Story story = getItem(position);

        TextView tvStoryTitle = (TextView) convertView.findViewById(R.id.tv_story_title);
        tvStoryTitle.setText(story.getTitle());
        TextView tvStoryOpening = (TextView) convertView.findViewById(R.id.tv_story_opening);
        tvStoryOpening.setText(story.getSegment(0).getContents());

        ImageView ivUserPicture = (ImageView) convertView.findViewById(R.id.iv_user_picture);
        ivUserPicture.setTag(position);
        ivUserPicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Show user profile
            }
        });

        LinearLayout llStoryLayout = (LinearLayout) convertView.findViewById(R.id.story_layout);
        llStoryLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), StoryActivity.class);
                    intent.putExtra("story", story);
                getContext().startActivity(intent);
            }
        });

        return convertView;
    }
}

When I go to pull this information from my Intent in the receiving activity...

Intent intent = getIntent();
Story story = (Story) intent.getSerializableExtra("story");

... And then try to test my code, I get this:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference

And I have no idea why. I thought maybe removing 'final' from the story variable would get rid of it, but I can't access the variable from inside my inner OnClickListener class without it. Not sure what to do.

EDIT: Also, here's the code that shows that I've added a story...

public class OverviewActivity extends AppCompatActivity {

    ArrayList<Story> stories = new ArrayList<Story>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_overview);

        stories.add(new Story("Lorem Ipsum", "Firstname Lastname",
                new StorySegment("Firstname Lastname", "Lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet...")));

        StoryOverviewAdapter storyOverviewAdapter = new StoryOverviewAdapter(this, stories);
        ListView listView = (ListView) findViewById(R.id.story_overview_list);
        listView.setAdapter(storyOverviewAdapter);
    }
}
Varoun
  • 41
  • 1
  • 6
  • check out this too http://stackoverflow.com/questions/37646466/attempt-to-invoke-virtual-method-on-a-null-object-reference-for-video-id – brahmy adigopula Oct 04 '16 at 05:43
  • are you sure your story is not null? – Vivek Mishra Oct 04 '16 at 05:45
  • Please, First check that your Serializable object is not null then after you pass into intent – Farmer Oct 04 '16 at 05:53
  • I just updated the original question with the chunk of code that passes the stories ArrayList to my Adapter. It has only one entry in it, but it still shouldn't be null. – Varoun Oct 04 '16 at 05:57
  • Managed to fix it. I was trying to get the SerializableExtras outside of the onCreate method, but it needs to be inside of it. – Varoun Oct 04 '16 at 06:29

4 Answers4

2

I managed to fix this issue by migrating the attempt to get the intent's extras...

    Intent intent = getIntent();
    story = (Story) intent.getSerializableExtra("story");

into my onCreate() method, instead of outside of it. So instead of this...

Intent intent = getIntent();
Story story = (Story) intent.getSerializableExtra("story");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_story);

... I now have this ...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_story);

    Intent intent = getIntent();
    Story story = (Story) intent.getSerializableExtra("story");
}

Was a silly oversight on my part... duh!

Varoun
  • 41
  • 1
  • 6
1

You can send custom objects as plain texts as well using GSON. No Serialization required.

  String intentData = gson.toJson(storyObject);
  intent.putExtra("story", intentData);

And in the receiving activity you can write as,

  Story object = gson.fromJson(intentData, Story.class);
Rohit J
  • 107
  • 7
  • Thanks for this! Still new to most of this and I've been told that serialization can be a suboptimal solution, so I'll definitely check this out. – Varoun Oct 04 '16 at 06:52
0

change this

Intent intent = new Intent(v.getContext(), StoryActivity.class);
                    intent.putExtra("story", story);
                getContext().startActivity(intent);

into this

Intent intent = new Intent(v.getContext(), StoryActivity.class);
                    intent.putExtra("story", (Serializable)stories.get(position));
                getContext().startActivity(intent);
Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
0

Use this code it works fine

private Context ctx;
public StoryOverviewAdapter(Context ctx, ArrayList<Story> stories) {
        super(ctx, 0, stories);
 this.ctx=ctx;
    }

Intent intent = new Intent(ctx, StoryActivity.class);
                    intent.putExtra("story", story);
                getContext().startActivity(intent);
Nitesh Pareek
  • 362
  • 2
  • 10