Reputation: 41
I am using BaseExpandableListAdapter and trying to do a dynamic list, where elements appears and disappears when you check or uncheck other elements. The group is an ArrayList of an object tha I made calle Mode, and children are a HashMap like that: HashMap> children.
The problem comes when I actualize the list to add children and they are added perfectly, but when I check other elements and the new children must disappear the program give me a NullPointerException in the method getChildrenCount(). I am actually using the notifyDataSetChanged() in both cases, when I am adding and when I am removing.
This is a piece of my code (the important part is that if I find another Mode object with the same parent ID in the DB I must to delete the old one):
cbMode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isSelected = modeDao.rbIsAlreadySelected(currentChild);
if(!isSelected) {
//if checked uncheck all the children of the same parent AND delete the children
Mode oldMode = modeDao.isThereAnotherRB(currentChild.getIdFather());
if(oldMode != null){
uncheckTheOldCB(oldMode);
setFalseOnCb(oldMode);
modeDao.deleteMode(oldMode);
deleteModeChildren(oldMode);
}
//AND insert it on the DB
modeDao.setSelectedMode(currentChild);
currentChild.setChecked(true);
//if has children show them
if(currentChild.hasChildren() == 1){
setNewFatherMode(currentChild);
}
}else {
//if unchecked delete this from the DB
modeDao.deleteMode(currentChild);
deleteModeChildren(currentChild);
currentChild.setChecked(false);
}
notifyDataSetChanged();
}
});
It shows as how I delete the children:
private void deleteModeChildren(Mode oldMode){
for (Map.Entry<Mode, ArrayList<Mode>> entry : children.entrySet()) {
Mode currentMode = entry.getKey();
if(currentMode.getId() == oldMode.getId()) {
children.remove(entry.getKey());
break;
}
}
}
And this is how I overrided the getChild method:
@Override
public Object getChild(int groupPosition, int childPosition) {
return this.children.get(this.parents.get(groupPosition))
.get(childPosition);
}
This it the track of the exception:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at com.iurban.iurbanapp.Adapters.NewModesAdapter.getChildrenCount(NewModesAdapter.java:63)
at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:563)
at android.widget.ExpandableListConnector.access$000(ExpandableListConnector.java:50)
at android.widget.ExpandableListConnector$MyDataSetObserver.onChanged(ExpandableListConnector.java:857)
at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
at android.widget.BaseExpandableListAdapter.notifyDataSetChanged(BaseExpandableListAdapter.java:56)
at com.iurban.iurbanapp.Adapters.NewModesAdapter$3.onClick(NewModesAdapter.java:223)
at android.view.View.performClick(View.java:4787)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19873)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
And this is the getChildCount method:
@Override
public int getChildrenCount(int groupPosition) {
return children.get(parents.get(groupPosition)).size();
}
Upvotes: 0
Views: 497
Reputation: 41
Ok, after a while I just to realise that I was deleting the children but not the corresponding parent of it. So, the getChildCount method gave me an error because the groupCount value were 4 when it must be 3.
Thank you all!
Upvotes: 1
Reputation: 553
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
This tells you that the size() method is what threw the NPE, so there's a good chance that it is the list which is null.
Double check that it is indeed initialised.
Upvotes: 0