I have made a simple add
method that takes two args.
Here is the code :
public List<myListType> add(List<myListType> myTypes, myListType myType) {
if (myTypes == null) {
myTypes = new ArrayList<myListType>();
}
int size = myTypes.size();
if (size > 1) {
Boolean lastStatus = myTypes.get(myTypes.size()-1).getAvailabilityStatus();
Boolean secondLastElement = myTypes.get(myTypes.size() - 2).getAvailabilityStatus();
if (lastStatus == null && secondLastElement == null) {
myTypes.remove(myTypes.size()-1);
}
else if (lastStatus.equals(secondLastElement)) {
myTypes.remove(myTypes.size()-1);
}
}
myTypes.add(myType);
return myTypes;
}
I think there is actually a possible null pointer deference in the if
condition, especially on lastStatus
. Do you see something I can do in order to avoid that ?
Thanks.
Assuming you're using java 7+, you can use
Objects.equals
, which takes care of both your conditions: