-1

I have a class cholesterol monitor that implements parcelable. In my activity, I try to pass an array list of parcelables via intent to another activity. I initialize my monitor list like this:

 private ArrayList<? extends Parcelable> monitor_list;

Then I pass it:

 monitor_list = patientListFragment.getMonitorList();
Intent intent = new Intent(this, CholesterolMonitorActivity.class);
            intent.putParcelableArrayListExtra("monitorList" ,monitor_list);
            startActivity(intent);

My get monitor list method returns an array list of cholesterol monitors:

 public ArrayList<CholesterolMonitor> getMonitorList(){
    return this.monitorList;
}

In my receiving activity I have two array lists,

private ArrayList<? extends Parcelable> monitor_list;
private ArrayList<CholesterolMonitor>cholesterol_monitor;

monitor_list= this.getIntent().getParcelableArrayListExtra("monitorList");

 this.cholesterol_monitor = (ArrayList<CholesterolMonitor>) monitor_list;

However, the app crashes upon this activities start up?This is the error log.

Process: com.example.safeheart, PID: 22778
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List org.hl7.fhir.dstu3.model.Patient.getName()' on a null object reference
    at com.example.safeheart.patientList.MonitorListRecyclerAdapter.onBindViewHolder(MonitorListRecyclerAdapter.java:41)
    at com.example.safeheart.patientList.MonitorListRecyclerAdapter.onBindViewHolder(MonitorListRecyclerAdapter.java:20)
    at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder
Sook Lim
  • 541
  • 6
  • 28
  • Post the error log – sanoJ May 17 '19 at 09:32
  • Ok now I'm guessing its returning something but the activity crashes after committing the fragments because we need to pass the array list into the fragments so I can't get Intent and commit the fragments in the same function onCreate? – Sook Lim May 17 '19 at 11:28

1 Answers1

0

You can do it like below

Create java object like below

public class ClassMyModel implements Serializable {
   private ArrayList<CholesterolMonitor> cholesterolMonitorArrayList;

    public ArrayList<CholesterolMonitor> getCholesterolMonitorArrayList() {
        return cholesterolMonitorArrayList;
    }

    public void setCholesterolMonitorArrayList(ArrayList<CholesterolMonitor> cholesterolMonitorArrayList) {
        this.cholesterolMonitorArrayList = cholesterolMonitorArrayList;
    }
}

And implement Serializable in your CholesterolMonitor Class also

public class CholesterolMonitor implements Serializable {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

In your Activity A

private ArrayList<CholesterolMonitor> cholesterolMonitorArrayList = new ArrayList<>();

            CholesterolMonitor test = new CholesterolMonitor();
            test.setName("test Name");
            cholesterolMonitorArrayList.add(test);

            ClassMyModel data = new ClassMyModel();
            data.setCholesterolMonitorArrayList(cholesterolMonitorArrayList);

            Intent intent = new Intent(SignUpActivity.this, ForgotPwdEmailActivity.class);
            intent.putExtra("TEST", data);
            startActivity(intent);

In your Activity B

       if (getIntent() != null) {
            classMyModel = (ClassMyModel) getIntent().getSerializableExtra("TEST");
            if(classMyModel != null){
                System.out.println("Name :: "+ classMyModel.getCholesterolMonitorArrayList().get(0).getName());
            }
        }

Note: Make sure each nested class of ClassMyModel has implemented Serializable interface

Dulanga
  • 921
  • 11
  • 23