Can an Arraylist of Custom Objects contain arraylists of other custom objects? And how would that work?
I tried:
public class BankAccount {
private String mTitle;
private float mBalance;
private ArrayList<sE> mHistory;
public BankAccount(String title) {
mTitle = title;
mPrefFile = null;
mBalance = 0;
mHistory = new ArrayList<>();
}
public void changeBalance(float newBalance) {
mBalance = newBalance;
}
public String getTitle() {
return mTitle;
}
public String getPrefFile() {
return mPrefFile;
}
public float getBalance() {
return mBalance;
}
public ArrayList<sE> getHistoryArrayList() {
return mHistory;
}
public void addToHistoryArrayList(String Title, Boolean IsEarning, float Amount) {
mHistory.add(new sE(Title, IsEarning, Amount));
}
//sE stands for Spending/Earning
public class sE {
private String mTitle;
private Boolean mIsEarning;
private float mAmount;
public sE(String Title, Boolean IsEarning, float Amount) {
mTitle = Title;
mIsEarning = IsEarning;
mAmount = Amount;
}
public String getTitle() {
return mTitle;
}
public Boolean getIsEarning() {
return mIsEarning;
}
public float getAmount() {
return mAmount;
}
}
}
But when trying to add an sE object to the History Array it gives me an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
How can I fix that? thanks for any help!