-1

I am working on an exercise where i build a bank system, I created an account class, a client class and a bank class, however when I check the program I get:

Exception in thread "main" java.lang.NullPointerException.

Here are my classes:

package bank.elements;

public class Account {

    private int id;
    private float balance;

    //constructors
    public Account(int id, float balance){
        this.setId(id);
        this.setBalance(balance);
    }

    public Account (int id){
        this.id=id;
        this.setBalance(0);
    }


    //getters
    public int getId(){
        return this.id;

    }

    public float getBalance(){
        return this.balance;
    }


    //setters
    public void setBalance(float balance){
        this.balance+=balance;
    }

    public void setId(int id){
        this.id=id;
    }

}



package bank.elements;

public class Client {

    private int id;
    private String name;
    private String rank;
    private float balance;
    private Account[] accounts = new Account[100];

    public Client(){

    }

    //getters
    public int getId(){
        return id;
    }

    public String getName(){
        return name;
    }

    public String getRank(){
        return rank;
    }

    public float getBalance(){
        return balance;
    }

    public float getAccountBalance(int id){

        float balance = 0;
        for (int i = 0; i < accounts.length; i++) {
            if(accounts[i].getId()==id){
                balance = accounts[i].getBalance();
            }
        }
        return balance;
    }

    public float getFortune(){
        float sum = 0;
        for (int i = 0; i < accounts.length; i++) {
            sum+=getAccountBalance(i);
        }
        sum+=getBalance();
        return sum;
    }

    //setters
    public void setName(String name){
        this.name = name;
    }

    public void setBalance(float balance){
        this.balance+=balance;
    }

    public void setId(int id){
        this.id=id;
    }

    public void addAccount(int id){
        for (int i = 0; i < accounts.length; i++) {
            if(accounts[i]==null){
                accounts[i].setId(id);
            }
        }
    }

}



package bank.elements;

public class Bank {

    public Bank(){

    }

    public Client[] clients = new Client[100];

    public float getBalance(){
        float sum = 0;

        for (int i = 0; i < clients.length; i++) {
            sum+= clients[i].getFortune();
        }
        return sum;
    }

    public void addClient(){
        for (int i = 0; i < clients.length; i++) {
            if(clients[i] == null){
                clients[i].setId(i);
            }
        }

    }

    public void removeClient(int id){
        for (int i = 0; i < clients.length; i++) {
            if(clients[i].getId()==id){
                clients[i]=null;
                for (int j = (i+1); j < clients.length; j++) {
                    clients[j] = clients[j-1];
                }

            }
        }
    }
}

And here is the Program thread:

package bank.program;

import bank.elements.Account;
import bank.elements.Client;
import bank.elements.Bank;

public class Program {
    public static void main(String[] args) {
        Bank b = new Bank();
        b.addClient();
        b.clients[0].setBalance(100);
        b.clients[0].addAccount(2);
        System.out.println(b.clients[0].getFortune());
    }
}

What am I missing?

ADTC
  • 7,367
  • 2
  • 57
  • 82
Ziv Kesten
  • 1,068
  • 20
  • 34
  • 3
    Where are you getting NPE. Kindly provide stacktrace and relevant code. – Aniket Thakur Jan 04 '14 at 13:44
  • [It's explained using C#, but many of the concepts are the same in Java. Please read through this first to understand what a NPE is.](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Jeroen Vannevel Jan 04 '14 at 13:51
  • 1
    i got it at the addClient() function line, my problem was not initializing the array with objects first, @user2336315 helped me understand it – Ziv Kesten Jan 04 '14 at 14:06

1 Answers1

3

In your addClient() method :

for (int i = 0; i < clients.length; i++) {
      if(clients[i] == null){
           clients[i].setId(i);
      }
 }

So that's totally normal the NPE is thrown because you check if the Client object at the ith position of your array is null and then you're trying to access a method from this object, and hence the NPE.

When you do public Client[] clients = new Client[100];, the 100 slots of the array are initialized to null (default value initialization of an Object in Java).

I would initialize it in your constructor :

public Bank(){
   for(int i = 0; i < clients.length; i++){
      clients[i] = new Client();
   }
}

Change your if in your addClient() method (do if client[i] != null to prevent the NPE), i.e :

for (int i = 0; i < clients.length; i++) {
      if (client[i] != null){
          clients[i].setId(i);
     }
 }

The same comment for your Client class with the array of Account.

Also consider to read this : java-What is a Null Pointer Exception?

Community
  • 1
  • 1
user2336315
  • 14,237
  • 9
  • 37
  • 63