Search code examples
androidandroid-fragmentsgreenrobot-eventbus

Green Robot Event Bus Post and Get ArrayList NPE


I have a fragment ui which has a recyclerview-checkbox list. This fragment is part of a tablayout of two tabs hosted in a dialog fragment. The hosting fragment contains a button from which the ids/names of the checked recyclerview items are collected and used.

I have used green robot to post a list containing the selected items and retrieved them in the hosting fragment on button click but I'm getting NPE.

Here's my implementation:

POJO Class:

public class MySessionEvents {

public List<String> strings;

public MySessionEvents(List<String> strings) {
    this.strings = strings;
}

public List<String> getStrings() {
    return strings;
}
}

Adapter Logic:

viewholder.checkBoxSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                Log.d(TAG, "Session id:\t" + sessions.prog_sessionId);
                selectedSessionsList.add(sessions.prog_sessionId);

                Bus.getBus().post(new MySessionEvents(selectedSessionsList));

                for (int m = 0; m < selectedSessionsList.size(); m++) {
                    Log.d(TAG, "Session id in list:\t" + selectedSessionsList.get(m));
                }
            }
        }
    });

and to retrieve the strings, I have registered and unregistered the bus(skipped the code) and done this:

@Subscribe
public void retrieveMySessions() {
    Log.d(TAG, "From event:\t" + events.getStrings());
     //for (int i = 0; i < events.strings.size(); i++){
    for (int i = 0; i < events.getStrings().size(); i++){
        Log.d(TAG, "My Session Ids:\t" + events.strings.get(i));
    }
}

Was I wrong to post from my adapter as the tab fragment was having NPE when trying to collect the checked items and also is my retrieval correct? Thanks.

EDIT:--- ADD PARAMS TO SUBSCRIBE METHOD ---

//    public void retrieveMySessions(MySessionEvents events) {
@Subscribe
public void retrieveMySessions() {
    Log.d(TAG, "From event:\t" + events.getStrings());
//  for (int i = 0; i < events.strings.size(); i++){
    for (int i = 0; i < events.getStrings().size(); i++){
        Log.d(TAG, "My Session Ids:\t" + events.strings.get(i));
    }
}

Solution

  • I solved this problem because the method anotated with @subscribe doesn't need to be called anywhere. I simply performed the operation in the method and got the values I sent with the bus like below:

    @Subscribe
    public void retrieveMySessions(MySessionEvents events) {
        myItemsList = events.getStrings();
    }
    

    In this case, I have added the items to the list and used where I wanted like this:

    NewProgramDialog dialog = new NewProgramDialog();
    
        Bundle bundle = new Bundle();
        String s = new Gson().toJson(myItemsList);
        bundle.putString(KEY_MY_SESSIONS, s);
        bundle.putBoolean("from_selections", true);
    
        dialog.setArguments(bundle);
        dialog.show(getChildFragmentManager(), "NewProgramDialog");
    
        dismiss();
    

    I used Gson to retrieve the values from the list. Problem solved. Thanks to all who responded.