0

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!

Ibrokhim Kholmatov
  • 1,079
  • 2
  • 13
  • 16
jonasxd360
  • 1,225
  • 4
  • 19
  • 35
  • mHistory = new ArrayList<>() before you can add object to it. – isaace Nov 24 '17 at 16:32
  • 2
    It indicates that the ArrayList is null when you're trying to add the new sE object to it. It does not make sense because you've already initialised the ArrayList in the constructor. Are you sure this is your complete code, and there's nothing more to it? Somewhere the ArrayList becomes null in your code. – Abhinav Manchanda Nov 24 '17 at 16:34
  • I have an arraylist of BankAccount Objects, which I save to sharedpreferences with gson and json. I load and write to it and save the arraylist in one activity, then I want to display the Bank Account Objects in a activity, where you also can see the sE (History) Arraylist of the current bank account Object. – jonasxd360 Nov 24 '17 at 16:54
  • Can you provide more context for this? It is saying `mHistory` is `null` when calling `BankAccount.addToHistoryArrayList`. And we can't really see how that's possible right now. It's possible that `getHistoryArrayList()` is being used and then manipulating your `ArrayList`. That's the only other place I can see anything weird happening since the variable is declared `private`. – Michael Yaworski Nov 24 '17 at 17:39
  • I think I might have found a solution to my problem, I added this to the BankAccount.java class: if (mHistory == null){ mHistory = new ArrayList<>(); } – jonasxd360 Nov 25 '17 at 00:12
  • Thanks everyone for your help finding a solution! – jonasxd360 Nov 25 '17 at 17:43
  • 1
    Also, code which you posted in question would not produce a NullPointerException which you posted. You clearly aren't showing the actual code which throws the NPE. – M. Prokhorov Nov 26 '17 at 13:15

1 Answers1

1

The code above posted is correct. You are instanting new ArrayList in a constructor and then adding new sE into it.It's fine. But when you try to make nested ArrayList then you have to create new ArrayList element for each element you add.

ArrayList<ArrayList<sE>> nestedList = new  ArrayList<>();

And while adding

nestedList.add(new ArrayList<>());

And add an element to it using

nestedList.get(0).add(new sE(...));

That's it.

jabu.hlong
  • 2,194
  • 20
  • 21