I am trying to pass a custom class (Custom class is Juice), from a fragment to an activity. I saw an answer here but it is not working when i am trying to turn the json string back into a Juice object. Here is my code with the error message
Fragment
Intent intent = new Intent(getActivity().getBaseContext(), SelectionActivity.class);
Bundle bundle = new Bundle();
Gson gson = new Gson();
String json = gson.toJson(juices.get(position));
Log.e("tree",json);
intent.putExtra("position",json);
getActivity().startActivity(intent);
Activity
Gson gson = new Gson();
Intent intent = getIntent();
if (getIntent().getStringExtra("position")!=null) {
String position = getIntent().getStringExtra("position");
Juice juice =gson.fromJson(position, Juice.class);
TextView textTitle = (TextView) findViewById(R.id.selectedTitle);
textTitle.setText(juice.getName());
ImageView imageView = (ImageView) findViewById(R.id.selected);
imageView.setImageResource((int) juice.getDrawable());
TextView textDescription = (TextView) findViewById(R.id.selectedDescription);
textDescription.setText(juice.getDescription());
}
Error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.intellidev.******/com.intellidev.*****.SelectionActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.intellidev.******.Juice.getDescription()' on a null object reference