What I’m trying to do is basically delete one position in the hashtable and then save that hashtable to a file on the user’s phone. The hashtable only has strings so no serialization is involved. The layout of the hashtable is as follows:
Key->Integer
Value->ArrayList()
In the arrayList is just string values.
The problem I have is when I delete one position in the arrayList, IF it was the last value in that arrayList that means that, That position in the hashtable has become null. And for some reason I can’t check for this case… I’ve tried various functions and none seem to work. Here is the code where I delete the value from the hashtable
// delete the assignment from the public list
for (int i = 0; i < allAssignments.size(); i++) {
//Here is where I get a null pointer-vvv
for (int p = 0; p < allAssignments.get(i).size(); p++) {
if (allAssignments.get(i).get(p).getTitle().equals(title)
&& allAssignments.get(i).get(p)
.getDate_due().equals(Date)) {
allAssignments.get(i).remove(p);
}
}
}
The deletion works perfectly fine if I delete positions in order like 4-3-2-1, but if I try and delete the 1st position and then try to delete the 2nd position it will give me a null pointer because it’s trying to access the 1st position first. Any suggestions?
this checks for null.
Simliar question