NullPointerException with a bank account in Java -


i exception when try create new customer in bank account program:

exception in thread "main" java.lang.nullpointerexception     @ bank.program.customer.addaccount(customer.java:18)     @ bank.program.bankprogram.main(bankprogram.java:24) java result: 1 

what mean, , need change rid of it?

bank class:

public class bank {     string bankname;         private arraylist<customer> customers = new arraylist<customer>();        bank(string bankname) {         this.bankname = bankname;     }      public void addcustomer(customer newcustomer) {         customers.add(newcustomer);     } } 

account class:

public abstract class account {     protected double balance = 0;     protected string accountid;      public account() {}  //defaultkonstruktor      public account(double bal, string id) {   //konstruktor         if (balance >= 0) {            balance = bal;         }         else {             balance = 0;         }         accountid = id;     }      public abstract void deposit(double amount);          public abstract void withdraw(double amount);      public abstract double getbalance();      public abstract string getaccountid();      public void transfer(double amount, account account) {         account.deposit(amount);         balance -= amount;     } } 

savingsaccount class:

public class savingsaccount extends account {     private double interest = 2.9;      public savingsaccount() {              super();     }      public savingsaccount(double bal, string id) {            super(bal, id);     }      @override     public void deposit(double amount) {      }      @override     public void withdraw(double amount) {      }      @override       public double getbalance() {      }      @override     public string getaccountid() {      } } 

customer class:

public class customer {     private string firstname;     private string lastname;     private string number;           private arraylist<account> accounts;      customer(string firstname, string lastname, string number) {          this.firstname = firstname;         this.lastname = lastname;         this.number = number;     }      public void addaccount(savingsaccount account) {         accounts.add(account);     }      public arraylist<account> getaccounts() {         return accounts;     } } 

bank program class:

public class bankprogram {     public static void main(string[] args) {          scanner input = new scanner(system.in);         int numberofcustomers = 0;         bank bank = new bank("bank name");          system.out.print("enter amount deposit: ");         double amount = input.nextdouble();         system.out.println("account number: " + numberofcustomers);         system.out.print("first name: ");         string firstname = input.next();         system.out.print("last name: ");         string lastname = input.next();         system.out.print("customer number: ");         string pnumber = input.next();          customer newcustomer = new customer(firstname, lastname, pnumber);                 savingsaccount savingsaccount = new savingsaccount(amount, "11");         newcustomer.addaccount(savingsaccount);         bank.addcustomer(newcustomer);                }     } 

you're trying add account without initializing arraylist in they're stored. add customer constructor.

this.accounts = new arraylist<account>(); 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -