eclipse运行显示Exception in thread "main" java.lang.NullPointerException at work_7.Bank.Openuser(Test.java:46) at work_7.Test.main(Test.java:12)

eclipse运行显示Exception in thread "main" java.lang.NullPointerException at work_7.Bank.Openuser(Test.java:46) at work_7.Test.main(Test.java:12)
拜求各位大佬,帮忙解决一下!!!!
图片说明

public class Test {
public static void main(String []args){
    Bank bank = new Bank();
    bank.Openuser("丁老头","12345",100.1);
    bank.Openuser("丁老太","234567",200.5);
    bank.Openuser("丁小胖","3456778",300.2);
    bank.SearchMoney("丁小胖");
    Account account = new Account("丁小女","567896",500.43);
    account.SaveMoney(500.123);
    account.DrawMoney(1000);
}
}


class Account{
    String username;
    String password;
    double money;
    Account(String username,String password,double money)
    {
        this.username=username;
        this.password=password;
        this.money=money;
    }
    void SaveMoney(double addmoney){
        this.money+=addmoney;
        System.out.println(username +" : 存钱以后,"+money);

    }
    void DrawMoney(double decmoney){
        this.money-=decmoney;
        System.out.println(username +" : 取钱以后,"+money);
    }
}





class Bank{
    String name;
    ArrayList<Account> list;
    void Openuser(String username,String password,double money){
        try{list.add(new Account(username,password,money));}
        catch(Exception ex){System.out.println("插入异常");}

    }
    void SearchMoney(String username){
        for(int i=0;i<list.size();i++)
        {
            if(list.get(i).username==username){
                System.out.println(username+" : "+"剩余金额为, "+list.get(i).money);
            }
        }
    }
}

2个回答

你的Bank类ArrayList list;集合没有初始化,在方法里初始化一下 list = new ArrayList;

niuniulamb
niuniulamb 非常感谢大佬!!!!
3 个月之前 回复
hongchenshijie
红尘丶世界 确实是这个的问题
3 个月之前 回复

断点调试,看为什么空指针, 而且代码能给全么? 代码不给全怎么看出问题在哪? 把你的 Bank 类和 Bank 类中的 Openuser方法截个图

niuniulamb
niuniulamb 嗯嗯好的,已经把代码都复制过来了,望大佬帮忙看看
3 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐