-1

I am getting list from adapter which is working fine. Now I need to store it in the list in MainActivity on button click. But I am getting crash as,

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.savedayreq.RoutePlanDayRequests.setPartnerId(java.lang.String)' on a null object reference at com.view.RoutePlanVisitChangeActivity$2.onClick(RoutePlanVisitChangeActivity.java:92)

line number 92 is this code.

 submitChangeRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                adapter = (RoutePlanVisitChangeAdapter) mRecyclerView.getAdapter();
                List<RoutePlanDayRequests> routePlanDayRequests=null;
                RoutePlanDayRequests requests = null;
                for (int i = 0; i < adapter.getItemCount(); i++) {
                    Retailers selectedView = adapter.getItem(i);

                    View dataView  = mRecyclerView.getChildAt(i);
                    Spinner toDaySpinner = (Spinner) dataView.findViewById(R.id.to_day);
                      //i can print this oldDay,newDay, msisdn line..
                    String oldDay = selectedView.getRoutePlanDayOfWeek(); 
                    String newDay = toDaySpinner.getSelectedItem().toString();
                    String msisdn = selectedView.getMsisdn(); //i can also print this line in log. 
        //***But I am getting crash from this line no 92..*** 
                    requests.setPartnerId(selectedView.getMsisdn());
                    requests.setParentPartnerId(selectedView.getMsisdn());
                    requests.setApproverId("1");
                    requests.setStatus("0");
                    requests.setRetailerPrmId(selectedView.getMsisdn());
                    requests.setOldDay(oldDay);
                    requests.setNeweDay(newDay);
                    routePlanDayRequests.add(requests); 
                    updateServiceList(AppUtils.getMsisdn(mSharedPreferences),routePlanDayRequests);
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Shiv
  • 129
  • 3
  • 12

3 Answers3

1

You are not initializing these 2 objects. They are set to null.

List<RoutePlanDayRequests> routePlanDayRequests=null;
RoutePlanDayRequests requests = null;

Initialize both of them like this -

List<RoutePlanDayRequests> routePlanDayRequests= new Arraylist();
RoutePlanDayRequests requests = new RoutePlanDayRequests();

You'll need a public constructor for you RoutePlanDayRequests. In you RoutePlanDayRequests class add this constructor.

public RoutePlanDayRequests() {
}
Ranjan
  • 1,096
  • 10
  • 22
0

You should make a new object from RoutePlanDayRequests and then you can set this object's variables change this line :

 RoutePlanDayRequests requests = null

to

RoutePlanDayRequests requests = new RoutePlanDayRequests()

if RoutePlanDayRequests has empty cunstructor

Reza.Abedini
  • 2,227
  • 2
  • 16
  • 18
0

Initialise this RoutePlanDayRequests class

  RoutePlanDayRequests requests = new RoutePlanDayRequests();
Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39