i am trying to write a code that stimulates an simple ATM. i have two class, one for the Bank Account and another for the ATM itself.

public BankAccount(String name1, int acctNum, int pinNum ){
			name = name1;
			accountNumber = acctNum;
			pinNumber = pinNum;
			balance = 100.00;
		}

public ATM(){
			start();
		}

but there is an error in the ATM constructor. it says that 'Implicit super constructor BankAccount() is undefined. Must explicitly invoke another constructor'.
can any one explain this to me? thanks.

I dont think you can define both constructors within the same class. You should write out the definition for the BankAccount constructor within the BankAccount class, and the same for the ATM class. You can create new instances of each method and it will run the constructor when you instantiate it. Or you can implement one of the classes onto the other and explicitly call the super constructor

Does you ATM class extends the BankAccount?

If yes then in you ATM class you define only the ATM() constructor, without a super() inside it. Since you don't have a constructor like:
BankAccount() in the super class, you get an error.

Try adding this constructor in the BankAccount:

public BankAccount() {

}

and then:

public ATM(){
                        super();
			start();
		}

OR change the ATM() into this:

public ATM(String name1, int acctNum, int pinNum){
                        super(name1, acctNum, pinNum);
			start();
		}

Of course the above are only correct if ATM extends BankAccount. If not, we will require to see some code.
And also I don't think the ATM should extend the BankAccount. I think that the ATM should have a collection (array, Vector) of BankAccounts

I dont think you can define both constructors within the same class. You should write out the definition for the BankAccount constructor within the BankAccount class, and the same for the ATM class. You can create new instances of each method and it will run the constructor when you instantiate it. Or you can implement one of the classes onto the other and explicitly call the super constructor

Fuze, thanks for the reply. i didn't put both the constructors in the same, of course. i have a BankAccount class and a ATM class.

Does you ATM class extends the BankAccount?

If yes then in you ATM class you define only the ATM() constructor, without a super() inside it. Since you don't have a constructor like:
BankAccount() in the super class, you get an error.

Try adding this constructor in the BankAccount:

public BankAccount() {

}

and then:

public ATM(){
                        super();
			start();
		}

OR change the ATM() into this:

public ATM(String name1, int acctNum, int pinNum){
                        super(name1, acctNum, pinNum);
			start();
		}

Of course the above are only correct if ATM extends BankAccount. If not, we will require to see some code.
And also I don't think the ATM should extend the BankAccount. I think that the ATM should have a collection (array, Vector) of BankAccounts

Thanks for the reply.
this is my ATM class, so far:

public class ATM  extends BankAccount{

		BankAccount []account = new BankAccount[10];

		public static void main ( String[] args){
			new ATM();
		}
		public ATM(){
			super();
			start();
		}
		public String start(){

			openNewAccount();
			displayWelcomeScreen();
			readCard();
			checkCard();
			JOptionPane.showInputDialog(null, "Enter Pin Number:", "ITB's ATM", JOptionPane.INFORMATION_MESSAGE );
			getMenu();
			
		return null;
		}
		public int getMenu(){

			String []options = { "Withdraw", "Deposit" , "Check balance", "exit"};
			int option = JOptionPane.showOptionDialog(null, "Hello! What would like to do?", "Menu Options", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);

			if (option == 0)
				withdrawCash();
				else if (option == 1)
				depositCash();
				else if (option == 3)
				getBalance();
			else {
				JOptionPane.showMessageDialog(null, "Thank you for using the ATM.");
			}
			return option;
		}

		public void displayWelcomeScreen(){
			String msg = " Welcome! \nPlease insert your ATM card...";
			JOptionPane.showMessageDialog( null, msg, "ITB's ATM", JOptionPane.INFORMATION_MESSAGE);
		}

		public void readCard(){

			JOptionPane.showInputDialog(null, "Please Enter account number");
		}

		public void checkCard(){

		}

		public String openNewAccount(){
			int recordAccounts = 0;
			for ( int i = 0; i<account.length; i++){
				if (account[i]== null && i < 10){
					recordAccounts = i;
					name = JOptionPane.showMessageDialog(null, "Please enter account name:");
					
				}
	}

	}

	}

and for my BankAccount class,

public class BankAccount {

		public String name;
		public int accountNumber;
		public int pinNumber;
		public double balance;

		public BankAccount(String name1, int acctNum, int pinNum ){
			name = name1;
			accountNumber = acctNum;
			pinNumber = pinNum;
			balance = 500.00;
		}
		public void withdrawCash(){
			String amountWithdraw = JOptionPane.showInputDialog(null, "How much would you like to withdraw fro your account?");
			float input = Float.valueOf(amountWithdraw).floatValue();
			if( balance > input ){
				balance = balance - input;
			}else{
				JOptionPane.showMessageDialog(null, "Sorry, Insufficient Funds!");
			}
		}

		public void depositCash(){

		}
		public double getBalance(){
			return balance;
		}
		public double setBalance(){
			return balance;
		}

		public float checkBalance(){

		}


	}

some of my methods aren't complete because i still can't figure out what to do with each method. some have errors..
can you help me with this.. thanks so much.

pardon me, cause i didn't get what you mean that it would be better if i didn't extend my bank account class? i f don't extend it, i will have errors in withdraw() and others

part of the instructions is to extend the ATM program by defining a couple of specialized account types. Each specialized account type should be derived from the account base class using inheritance. ==> my specialized account types are the savings account and checking account. Does this mean that i have to create these then extend Bank Account on it? how do you use inheritance anyway?
I am partly confused.. please help.. thanks

Thanks for the reply.
this is my ATM class, so far:

public class ATM  extends BankAccount{

		BankAccount []account = new BankAccount[10];

		public static void main ( String[] args){
			new ATM();
		}
		public ATM(){
			super();
			start();
		}
		public String start(){

			openNewAccount();
			displayWelcomeScreen();
			readCard();
			checkCard();
			JOptionPane.showInputDialog(null, "Enter Pin Number:", "ITB's ATM", JOptionPane.INFORMATION_MESSAGE );
			getMenu();
			
		return null;
		}
		public int getMenu(){

			String []options = { "Withdraw", "Deposit" , "Check balance", "exit"};
			int option = JOptionPane.showOptionDialog(null, "Hello! What would like to do?", "Menu Options", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);

			if (option == 0)
				withdrawCash();
				else if (option == 1)
				depositCash();
				else if (option == 3)
				getBalance();
			else {
				JOptionPane.showMessageDialog(null, "Thank you for using the ATM.");
			}
			return option;
		}

		public void displayWelcomeScreen(){
			String msg = " Welcome! \nPlease insert your ATM card...";
			JOptionPane.showMessageDialog( null, msg, "ITB's ATM", JOptionPane.INFORMATION_MESSAGE);
		}

		public void readCard(){

			JOptionPane.showInputDialog(null, "Please Enter account number");
		}

		public void checkCard(){

		}

		public String openNewAccount(){
			int recordAccounts = 0;
			for ( int i = 0; i<account.length; i++){
				if (account[i]== null && i < 10){
					recordAccounts = i;
					name = JOptionPane.showMessageDialog(null, "Please enter account name:");
					
				}
	}

	}

	}

and for my BankAccount class,

public class BankAccount {

		public String name;
		public int accountNumber;
		public int pinNumber;
		public double balance;

		public BankAccount(String name1, int acctNum, int pinNum ){
			name = name1;
			accountNumber = acctNum;
			pinNumber = pinNum;
			balance = 500.00;
		}
		public void withdrawCash(){
			String amountWithdraw = JOptionPane.showInputDialog(null, "How much would you like to withdraw fro your account?");
			float input = Float.valueOf(amountWithdraw).floatValue();
			if( balance > input ){
				balance = balance - input;
			}else{
				JOptionPane.showMessageDialog(null, "Sorry, Insufficient Funds!");
			}
		}

		public void depositCash(){

		}
		public double getBalance(){
			return balance;
		}
		public double setBalance(){
			return balance;
		}

		public float checkBalance(){

		}


	}

some of my methods aren't complete because i still can't figure out what to do with each method. some have errors..
can you help me with this.. thanks so much.

One problem with the ATM account in the constructor is the way you call the 'super()' constructor.
This statement attempts to call the constructor of the parent class, in this case BankAccount. The problem is the BankAccount class has a constructor with the signature requiring a (String , int , int) parameters.

I still don't understand why the ATM should extends the BankAccount. You were right to have an array of BankAccounts in the ATM class, so what would be the point of extending.

Also I believe that you need to store the Pin number that you get and use this in order to find the BankAccount you want to manipulate in the getMenu() method

I still don't understand why the ATM should extends the BankAccount. You were right to have an array of BankAccounts in the ATM class, so what would be the point of extending.

Also I believe that you need to store the Pin number that you get and use this in order to find the BankAccount you want to manipulate in the getMenu() method

1. Cause if i don't extend it.. i have errors in my ATM class.
2. How do i store the pin number and account number? in arrays? i can't get it.. the program should start by initializing and declaring a single atm object which in turn initializes it's account objects.. so what i did is before the menu, the user has to create a new account? but i can't seem to figure it...

1. Cause if i don't extend it.. i have errors in my ATM class.
2. How do i store the pin number and account number? in arrays? i can't get it.. the program should start by initializing and declaring a single atm object which in turn initializes it's account objects.. so what i did is before the menu, the user has to create a new account? but i can't seem to figure it...

I agree with javaAddict. Don't have ATM extend BankAccount. An ATM is not a type of BankAccount. It has a relationship to a bank account, but it's not an "is a" relationship.

The error (or at least AN error) would appear to be this:

public ATM(){
			super();
			start();
		}

If ATM doesn't extend something, it has no superclass, so hence, a call to the non-existing superclass is an error. The solution is not to extend BankAccount, but instead to take out or change the call to super (), plus whatever other calls you have in the program that give you errors when you remove the "extends" part. As for how to store account numbers, etc., you already have that in BankAccount. To access that from ATM without having ATM extend BankAccount, get a BankAccount object:

BankAccount ba = account[0];

then call whatever BankAccount function you want to call:

ba.getBalance ();

I agree with javaAddict. Don't have ATM extend BankAccount. An ATM is not a type of BankAccount. It has a relationship to a bank account, but it's not an "is a" relationship.

The error (or at least AN error) would appear to be this:

public ATM(){
			super();
			start();
		}

If ATM doesn't extend something, it has no superclass, so hence, a call to the non-existing superclass is an error. The solution is not to extend BankAccount, but instead to take out or change the call to super (), plus whatever other calls you have in the program that give you errors when you remove the "extends" part. As for how to store account numbers, etc., you already have that in BankAccount. To access that from ATM without having ATM extend BankAccount, get a BankAccount object:

BankAccount ba = account[0];

then call whatever BankAccount function you want to call:

ba.getBalance ();
public int menuOptions(){

			String []options = { "Withdraw", "Deposit" , "Check balance", "exit"};
			int option = JOptionPane.showOptionDialog(null, "Hello! What would like to do?", "Menu Options", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);

			if (option == 0)
				
				withdrawCash();
				else if (option == 1)
				depositCash();
				else if (option == 3)
				getBalance();
			else {
				JOptionPane.showMessageDialog(null, "Thank you for using the ATM.");
			}
			return option;
		}

ok.. i didn't extend my Atm to Bank Account class. You were right. then now the methods withdrawCash, depositCash, and getBalance has errors because it is defined in the Bank Account class..
??????
i tried doing this

BankAccount.withdrawCash();

it says that it can't make a static reference to a non static method withdrawCash. But my menuOptions() isn't static..

ALSO i had this method:

public void openNewAccount(){
			int i= 0;
				if (account[i]== null && i < 10){
					BankAccount.name = JOptionPane.showInputDialog(null,"Please enter account name:", "Create New Account", JOptionPane.INFORMATION_MESSAGE);
				 account[i] =name;
					String acctNum =JOptionPane.showInputDialog(null, "Please verify account number", "Create New Account", JOptionPane.INFORMATION_MESSAGE);
					String acctNum2 =JOptionPane.showInputDialog(null, "Please verify account number", "Create New Account", JOptionPane.INFORMATION_MESSAGE);
					if (acctNum == acctNum2){
						account[i].setAcctNum(acctNum2);
					}
					String pin = JOptionPane.showInputDialog(null, "Please enter pin number", "Create New Account", JOptionPane.INFORMATION_MESSAGE);
				}else{
					JOptionPane.showMessageDialog(null, "Sorry but we can't accomodate new accounts as of this moment.");
				}
			}

but it has the error in line 5 that can't convert String to BankAccount..

If you look at my example, note that I do this:

BankAccount ba = account[0];
ba.getBalance ();

rather than:

BankAccount.getBalance ();

In the first, ba is an INSTANCE of the BankAccount class, not the BankAccount class itself. You need to specify WHICH bank account you are using.

In your second question, it's hard to comment because I don't know which class (Bank Account or ATM) this openNewAccount function is now in. If it's in BankAccount still, then you can't use the account[] variable anymore since that's not in BankAccount. I imagine you want to assign the results of the showInputDialog to name, which is a String in line 4 and delete line 5.

That's assuming this is all in the BankAccount class. name is a String and showInputDialog returns a String, and you can't use the account[] data member in BankAccount since it's from ATM. You should repost both classes in their entirety so people know what you've done.

If you look at my example, note that I do this:

BankAccount ba = account[0];
ba.getBalance ();

rather than:

BankAccount.getBalance ();

In the first, ba is an INSTANCE of the BankAccount class, not the BankAccount class itself. You need to specify WHICH bank account you are using.

In your second question, it's hard to comment because I don't know which class (Bank Account or ATM) this openNewAccount function is now in. If it's in BankAccount still, then you can't use the account[] variable anymore since that's not in BankAccount. I imagine you want to assign the results of the showInputDialog to name, which is a String in line 4 and delete line 5.

That's assuming this is all in the BankAccount class. name is a String and showInputDialog returns a String, and you can't use the account[] data member in BankAccount since it's from ATM. You should repost both classes in their entirety so people know what you've done.

This is my ATM class:

import javax.swing.*;

public class ATM {

    BankAccount []account = new BankAccount[10];
    BankAccount ba;
    String input;

    public static void main ( String[] args){
        new ATM();
    }
    public ATM(){
        super();
        start();
    }
    public String start(){

        openNewAccount();
        displayWelcomeScreen();

    return null;
    }
    public int menuOptions(){

        String []options = { "Withdraw", "Deposit" , "Check balance", "exit"};
        int option = JOptionPane.showOptionDialog(null, "Hello! What would like to do?", "Menu Options", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);

        if (option == 0)
            ba.withdrawCash();
            else if (option == 1)
            ba.depositCash();
            else if (option == 3)
            ba.getBalance();
        else {
            JOptionPane.showMessageDialog(null, "Thank you for using the ATM.");
        }
        return option;
    }

    public void displayWelcomeScreen(){
        String msg = " Welcome! \nPlease insert your ATM card...";
        input = JOptionPane.showInputDialog( null, msg, "ITB's ATM", JOptionPane.INFORMATION_MESSAGE);
            if ( input.equals(BankAccount.accountNumber)) {
                String inputPin = JOptionPane.showInputDialog(null, "Enter Pin Number:", "ITB's ATM", JOptionPane.INFORMATION_MESSAGE );
                if (inputPin.equals(BankAccount.pinNumber)){
                    menuOptions();
                }
            }else{
                JOptionPane.showInternalMessageDialog(null, "Error! Please insert card again");
                displayWelcomeScreen();
            }
    }

    public void openNewAccount(){
        int i= 0;
            if (account[i]== null && i < 10){
                String name = JOptionPane.showInputDialog(null,"Please enter account name:", "Create New Account", JOptionPane.INFORMATION_MESSAGE);
                String acctNum =JOptionPane.showInputDialog(null, "Please enter account number", "Create New Account", JOptionPane.INFORMATION_MESSAGE);
                String acctNum2 =JOptionPane.showInputDialog(null, "Please verify account number", "Create New Account", JOptionPane.INFORMATION_MESSAGE);
                if (acctNum == acctNum2){
                    account[i].setAcctNum(acctNum2);
                }
                String pin = JOptionPane.showInputDialog(null, "Please enter pin number", "Create New Account", JOptionPane.INFORMATION_MESSAGE);
                    account[i].setPin(pin);
            }else{
                JOptionPane.showMessageDialog(null, "Sorry but we can't accomodate new accounts as of this moment.");
            }
        }
    }

and this is my Bank Account class :

import javax.swing.*;

public class BankAccount {


    public static String name;
    public static int accountNumber;
    public static int pinNumber;
    public static float balance;

    public BankAccount(){
        balance = 500;
    }

    public void withdrawCash(){
        String []options = {"Savings Account", "Checking Account"};
        int opt = JOptionPane.showOptionDialog(null, "Where do you want to withraw?",null, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);
        if (opt == 0){
            withdrawCash();
        }else{
            withdrawCash();
        }

        String amountWithdraw = JOptionPane.showInputDialog(null, "How much would you like to withdraw from your account?");
        float input = Float.valueOf(amountWithdraw).floatValue();
        if( balance > input ){
            balance = balance - input;
        }else{
            JOptionPane.showMessageDialog(null, "Sorry, Insufficient Money!");
        }
    }

    public void depositCash(){
        String []options = {"Savings Account", "Checking Account"};
        int opt = JOptionPane.showOptionDialog(null, "Where do you want to withraw?",null, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);
        if (opt == 0){
            depositCash();
        }else{
            depositCash();
        }
    }

    public float getBalance(){

        return balance;
    }

    public double setBalance(){
        return balance;
    }

    public float checkBalance(){
        getBalance();
        return balance;
    }

    public int setAcctNum(String acctNum2){
        accountNumber = Integer.parseInt(acctNum2);
        return accountNumber;
    }

    public int getAcctNum(){
        return accountNumber;
    }
    public void setPin(String pin){
        pinNumber = Integer.parseInt(pin);
    }

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I still can't run the program properly.. I have the idea but can't seem to figure out which one goes.. i want to put all bank account transactions in the bank class. My bank account class also has to extend two other specialized class, savings account class and checking account class..
I don't know if in my openNewAccount(), i was able to store name, account number and pin number somewhere..

Using a variable called ba was just an example. It won't work with your code since you haven't assigned ba to be a particular account. You have the right idea with these calls:

account[i].setAcctNum(acctNum2);

account is an Object of type BankAccout, which you want. The problem there is acctNum2 is a String, not an int, since you got it from showInputDialog, which returns a String. Use parseInt to convert it to an integer.

Using a variable called ba was just an example. It won't work with your code since you haven't assigned ba to be a particular account. You have the right idea with these calls:

account[i].setAcctNum(acctNum2);

account is an Object of type BankAccout, which you want. The problem there is acctNum2 is a String, not an int, since you got it from showInputDialog, which returns a String. Use parseInt to convert it to an integer.

i think i did it in my bank account class already. or maybe you think is should convert it in my openNewAccount()?

can anyone help me with the codes, it still isn't finish? i still haven't got to the withdraw and deposit transaction cause when i try to run the program, i has some run time errors. I am stuck in the open new account process... i don't know where to store the input name.. i was thinking it should be in the account, but account is an object of my BankAccount. I can't convert string to BankAccount. or maybe my concept is all wrong... please help????

Do you not understand Vernon's point about the variable ba? Learn the basics first, then work your way up.

Btw, what are you referring to when you say 'input name'? Also, post your code that you think is incorrect, or ask a specific question. And make sure to use code tags

Do you not understand Vernon's point about the variable ba? Learn the basics first, then work your way up.

Btw, what are you referring to when you say 'input name'? Also, post your code that you think is incorrect, or ask a specific question. And make sure to use code tags

this is my atm class..

import javax.swing.*;

public class ATM {

BankAccount []account = new BankAccount[10];
BankAccount ba = account[i];
String input;

public static void main ( String[] args){
new ATM();
}
public ATM(){
super();
start();
}
public String start(){

openNewAccount();
displayWelcomeScreen();

return null;
}
public int menuOptions(){

String []options = { "Withdraw", "Deposit" , "Check balance", "exit"};
int option = JOptionPane.showOptionDialog(null, "Hello! What would like to do?", "Menu Options", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);

if (option == 0)
ba.withdrawCash();
else if (option == 1)
ba.depositCash();
else if (option == 3)
ba.getBalance();
else {
JOptionPane.showMessageDialog(null, "Thank you for using the ATM.");
}
return option;
}

public void displayWelcomeScreen(){
String msg = " Welcome! \nPlease insert your ATM card...";
input = JOptionPane.showInputDialog( null, msg, "ITB's ATM", JOptionPane.INFORMATION_MESSAGE);
if ( input.equals(BankAccount.accountNumber)) {
String inputPin = JOptionPane.showInputDialog(null, "Enter Pin Number:", "ITB's ATM", JOptionPane.INFORMATION_MESSAGE );
if (inputPin.equals(BankAccount.pinNumber)){
menuOptions();
}
}else{
JOptionPane.showInternalMessageDialog(null, "Error! Please insert card again");
displayWelcomeScreen();
}
}

public void openNewAccount(){
int i= 0;
if (account[i]== null && i < 10){
String name = JOptionPane.showInputDialog(null,"Please enter account name:", "Create New Account", JOptionPane.INFORMATION_MESSAGE);
String acctNum =JOptionPane.showInputDialog(null, "Please enter account number", "Create New Account", JOptionPane.INFORMATION_MESSAGE);
String acctNum2 =JOptionPane.showInputDialog(null, "Please verify account number", "Create New Account", JOptionPane.INFORMATION_MESSAGE);
if (acctNum == acctNum2){
account[i].setAcctNum(acctNum2);
}
String pin = JOptionPane.showInputDialog(null, "Please enter pin number", "Create New Account", JOptionPane.INFORMATION_MESSAGE);
account[i].setPin(pin);
}else{
JOptionPane.showMessageDialog(null, "Sorry but we can't accomodate new accounts as of this moment.");
}
}
}

and this is Bank Account class..

import javax.swing.*;

public class BankAccount {


public static String name;
public static int accountNumber;
public static int pinNumber;
public static float balance;

public BankAccount(){
balance = 500;
}

public void withdrawCash(){
String []options = {"Savings Account", "Checking Account"};
int opt = JOptionPane.showOptionDialog(null, "Where do you want to withraw?",null, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);
if (opt == 0){
withdrawCash();
}else{
withdrawCash();
}

String amountWithdraw = JOptionPane.showInputDialog(null, "How much would you like to withdraw from your account?");
float input = Float.valueOf(amountWithdraw).floatValue();
if( balance > input ){
balance = balance - input;
}else{
JOptionPane.showMessageDialog(null, "Sorry, Insufficient Money!");
}
}

public void depositCash(){
String []options = {"Savings Account", "Checking Account"};
int opt = JOptionPane.showOptionDialog(null, "Where do you want to withraw?",null, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);
if (opt == 0){
depositCash();
}else{
depositCash();
}
}

public float getBalance(){

return balance;
}

public double setBalance(){
return balance;
}

public float checkBalance(){
getBalance();
return balance;
}

public int setAcctNum(String acctNum2){
accountNumber = Integer.parseInt(acctNum2);
return accountNumber;
}

public int getAcctNum(){
return accountNumber;
}
public void setPin(String pin){
pinNumber = Integer.parseInt(pin);
}

}

Your class has the following variable:

BankAccount ba = account;

That doesn't make any sense. You never declared "i", so your program doesn't know what "i" is. In addition, lets imagine that you had declared i. For example, consider the following code:

BankAccount []account = new BankAccount[10];
BankAccount ba = account[0];

That assigns the value at account[0] to ba. Since the account array is empty, ba probably now contains null. Regardless of what ba contains, it is useless.

In the future please include error messages in your post...

I'm sorry about that.

here is the error:
Exception in thread "main" java.lang.NullPointerException
at Atm.openNewAccount(Atm.java:69)
at Atm.start(Atm.java:20)
at Atm.<init>(Atm.java:16)
at Atm.main(Atm.java:12)

Your class has the following variable:

BankAccount ba = account;

That doesn't make any sense. You never declared "i", so your program doesn't know what "i" is. In addition, lets imagine that you had declared i. For example, consider the following code:

BankAccount []account = new BankAccount[10];
BankAccount ba = account[0];

That assigns the value at account[0] to ba. Since the account array is empty, ba probably now contains null. Regardless of what ba contains, it is useless.

um.. i tried this

BankAccount ba = account[10]
whe i run it, it has this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Atm.<init>(Atm.java:8)
at Atm.main(Atm.java:12)

whew.. ok i think i get what null pointer means.. hmmmm.. but (gosh). it is in mu:
account.setPin(pin)

why does it do this?

um.. i tried this

BankAccount ba = account[10]
whe i run it, it has this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

When the array has size 10, you can go from 0 to 9:
BankAccount ba = account[9];


whew.. ok i think i get what null pointer means.. hmmmm.. but (gosh). it is in mu:
account.setPin(pin)

BankAccount ba = new BankAccount[10]; //the ARRAY ba is not null

But ba[0], ba[1], .... ARE null.
So I presume that you define the array account but you don't define the account[i] element

In addition to what JavaAddict said, you missed my point -


if you create an array of Objects, but do not give it any values, then it will be full of nothing. The way Java and other languages handle this is by initially making each element of the array 'null'. So if you said

BankAccount[] accounts = new BankAccount[10];
BankAccount mine = accounts[9];

It would still be a logical error because mine is now null.

This article has been dead for over six months. Start a new discussion instead.