0

I just started learning java and I'm trying to figure out the following requirement. I want to create a instances of my own class as an array. Then I want to refer each object using a loop and enter data. My class is "BankAccount". My instances are "acct[]". But when I try to insert data in to instances I get the following error. Can some one help me to identify the problem.

Error

My code is as follows;

public class BankAccount {

  private String accNo;
  private String name;
  private String type;
  private double balance;

  //Constructor
  public void BankAccount(String accNo, String name, String type, double balance) {
    this.accNo = accNo;
    this.name = name;
    this.type = type;
    this.balance = balance;
  }

  //Diposit
  public void diposit(double amount) {
    balance += amount;
  }

  //Withdraw
  public void withdraw(double amount) {
    if (amount < balance)
      balance -= amount;
    else
      System.out.println("Balnce not enough for the withdrawal");
  }

  //Display data
  public void display() {
    System.out.println("Acc No" + this.accNo);
    System.out.println("Name : " + this.name);
    System.out.println("Type : " + this.balance);
    System.out.println("balance : " + this.balance);
    System.out.println("-------------------------------------------------------------------");
  }
}
public class testBankAccount {

  public static void main(String[] args) {
    //Declaring 10 BankAccount objects as an array
    BankAccount[] acct = new BankAccount[10];

    //inserting data to each object using a loop
    for (int i = 1; i < 11; i++) {
      acct[i].BankAccount("Acc", "name", "type", 0);
      acct[i].diposit(1000);
      acct[i].withdraw(500);
      acct[i].display();
    }
  }
}
Chaosfire
  • 4,818
  • 4
  • 8
  • 23
  • In short you need to use the `new` keyword to initilize the BankAccount before interacting the the array like so `acct[i] = new BankAccount("Acc", "name", "type", 0);` instead of `acct[i].BankAccount("Acc", "name", "type", 0);` – sorifiend Jul 22 '22 at 05:25
  • As suggested by VinitSolanki, the other issue is that your `for` loop will also cause an error. Arrays start at 0, not 1, so change `for (int i=1; i<11; i++)` to be `for (int i=1; i<10; i++)` otherwise you will get an out of bounds error once you fix the null pointer error. – sorifiend Jul 22 '22 at 05:32
  • As per the [ask], please [**do not** post images of code, data, error message, etc](https://meta.stackoverflow.com/questions/285551). Instead, copy or type the text into your question, [formatted as code](https://meta.stackoverflow.com/questions/251361#251362). Reserve the use of images for diagrams or demonstrating rendering bugs; things that are impossible to describe accurately via text. – Bohemian Jul 22 '22 at 05:40

3 Answers3

3

You need to create the object, before you can work with it. BankAccount[] acct = new BankAccount[10]; creates the array, it does not create object in every position in the array. You need to create the object before you can invoke its' methods.

acct[i] = new BankAccount();

Other things to note:

  1. public void BankAccount is not a constructor, you have declared a method. Constructor looks like this
public BankAccount(String accNo, String name, String type, double balance) {
  //set fields
}
  1. Arrays indexes are zero based, meaning for (int i=1; i<11; i++) will skip first element and throw exception on the last iteration. Correct would be
for (int i=0; i<10; i++) {
 //do stuff
}
  1. You really should format your code properly(best to follow java conventions), you will make your own life easier when developing.
Chaosfire
  • 4,818
  • 4
  • 8
  • 23
1

I can see multiple issues there in the code as below.

  1. You have added the "constructor" comment just above BankAccount(String accNo, String name, String type, double balance), If you add a return type in the constructor then it will treat as a normal method. remove void from if you really want it constructor there.
  2. If BankAccount(String, String, String, double) is a constructor, you would be required to use a new keyword there to create an object and assign it to an array. eg. acct[I] = new BankAccount("Acc", "name", "type", 0);
  3. if BankAccount(String, String, String, double) is not a constructor and you want to make it to a normal method then before assigning the value to an array you are required to create a blank class object and for that you need to create a no-argument constructor and assign value to an array. e.g acct[I] = new BankAccount();
  4. you will get ArrayIndexOutOfBoundsException because you have started index from 1 till 10. array index starts from 0 so you would be required to use for (int i=0; i<10; i++)
Vinit Solanki
  • 1,863
  • 2
  • 15
  • 29
-1

you are running the loop from 1 to 10. But the array is accessible from 0 to 9

Fahad
  • 1
  • 1