Java 自定义对象的Arraylist是否可以包含其他自定义对象的Arraylist?

Java 自定义对象的Arraylist是否可以包含其他自定义对象的Arraylist?,java,android,arraylist,custom-object,Java,Android,Arraylist,Custom Object,自定义对象的Arraylist是否可以包含其他自定义对象的Arraylist?那怎么办 我试过: public class BankAccount { private String mTitle; private float mBalance; private ArrayList<sE> mHistory; public BankAccount(String title) { mTitle = title; mPref

自定义对象的Arraylist是否可以包含其他自定义对象的Arraylist?那怎么办

我试过:

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;
        }
    }
}
公共类银行账户{
私有字符串mTitle;
私人股本;
私人诉讼史;
公共银行账户(字符串标题){
mTitle=标题;
mPrefFile=null;
mBalance=0;
mHistory=新的ArrayList();
}
公共无效变动余额(浮动新余额){
mBalance=newBalance;
}
公共字符串getTitle(){
返回mTitle;
}
公共字符串getPrefFile(){
返回MPREF文件;
}
公共浮点数{
回归平衡;
}
公共ArrayList getHistoryArrayList(){
回归历史;
}
public void addToHistoryArrayList(字符串标题、布尔值IsEarning、浮动金额){
mHistory.添加(新sE(标题、iLearning、金额));
}
//sE代表支出/收入
公共类sE{
私有字符串mTitle;
私人布尔错误学习;
私人浮动马蒙特;
公共sE(字符串标题、布尔值IsEarning、浮动金额){
mTitle=标题;
错误学习=正在学习;
mAmount=金额;
}
公共字符串getTitle(){
返回mTitle;
}
公共布尔GetiLearning(){
返回错误学习;
}
公共浮动金额(){
返回mAmount;
}
}
}
但是,当尝试将sE对象添加到历史记录数组时,会出现错误:

java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“boolean java.util.ArrayList.add(java.lang.Object)”


我怎样才能解决这个问题?谢谢你的帮助

上面发布的代码是正确的。您正在构造函数中实例化新的ArrayList,然后向其中添加新的sE。这很好。但是,当您尝试创建嵌套的ArrayList时,必须为添加的每个元素创建新的ArrayList元素

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

就是这样。

mHistory=new ArrayList(),然后才能向其中添加对象。它表示在尝试向ArrayList添加新sE对象时,ArrayList为空。这没有意义,因为您已经在构造函数中初始化了ArrayList。您确定这是您的完整代码,没有其他内容了吗?在代码中的某个地方,ArrayList变为null。我有一个BankAccount对象的ArrayList,我用gson和json将其保存到SharedReferences。我在一个活动中加载、写入并保存arraylist,然后我想在一个活动中显示银行帐户对象,在这里您还可以看到当前银行帐户对象的sE(历史记录)arraylist。您能提供更多上下文吗?调用
BankAccount.addToHistoryArrayList
时,表示
mHistory
null
。我们现在还不知道这是怎么可能的。可能正在使用
getHistoryArrayList()
,然后操作
ArrayList
。因为变量被声明为
private
,所以这是我唯一能看到奇怪事情发生的地方。而且,您发布的有问题的代码不会产生您发布的NullPointerException。很明显,您没有显示抛出NPE.OP写入的实际代码,在将对象添加到历史记录时出错。为什么会出错?我必须在哪里编写此代码?(在BankAccount.java类中或在活动中添加时)感谢您的帮助!在BankAccount类本身中。假设嵌套的ArrayList位于代码中。为什么必须嵌套ArrayList?因为您询问“自定义对象的ArrayList是否可以包含其他自定义对象的ArrayList?”
nestedList.add(new ArrayList<>());
nestedList.get(0).add(new sE(...));