Hi Dw

I'm creating an atm appication and now my research pointed me to use the Java Point Of Service/Sale (JPOS) my application allow user to choose the amount from the options displayed on the screen and the user will use the number keypad to make his/her selection and the amount displayed on screen is retrieved from his/her account and it is splited in to 5 ways. Now if I run it, it gives me 5 errors under the Dispense amount, I've commented next to the line(s) where there are errors and I've wrote the error that I get as is on the comment in the codes.Please refer to the codes below.

/*
 *@author Mr Hacker
 */
import java.util.Scanner;
import CashDispenser;

public class Main {
private double currentBal =1899562589;
Scanner input = new Scanner(System.in);
private CashDispenser cashDispenser;
int amount;
public void mainMenu(){

    int selection;
        System.out.print("Welcome to the Automated Teller Machine!\n");
        System.out.println("Select from the following menu options below:\n");
        System.out.println("========================");
        System.out.println("| [1]  Check Balance   |");
        System.out.println("| [2]  Withdrawal      |");
        System.out.println("| [3]  Deposit         |");
        System.out.println("| [4]  Exit            |");
        System.out.println("========================");
        System.out.print("Please select your option now: ");
        selection =input.nextInt();


switch (selection){
    case 1:
        viewBalance();
        break;
    case 2:
        withdrawFunds();
        break;
    case 3:
        depositFunds();
        break;
    case 4:
        System.out.println(" Thank you for using This ATM today. Bye!");

}
}
public void viewBalance() {
    int selection1;
    System.out.println("You have selected Balance.\n");
    System.out.println("\t-- Your Current Balance is:R " + currentBal);
    System.out.println("Return to main menu? \n [1] for YES \n");
    selection1 =input.nextInt();
    switch (selection1){
    case 1:
        mainMenu();
        break;
}
}
public void withdrawFunds() {
    int withdrawSelection;
    System.out.println("Amount to withdraw: ");
    System.out.println("[1] - R10");
    System.out.println("[2] - R20");
    System.out.println("[3] - R50");
    System.out.println("[4] - R100");
    System.out.println("[5] - R200");
    System.out.println("[6] - MAIN MENU");
    System.out.print("Please select your option now: ");
    withdrawSelection =input.nextInt();

    boolean cashDispensed = false; // cash was not dispensed yet


    // Here I'm trying to dispense the choosen amount out of the device.

switch (withdrawSelection){
    case 1:
        accountWithdraw(10);
        amount = (10);
        CashDispenser.dispenseCash(amount); // My error starts here it says 'non-static method dispenseCash(int) cannot be referenced from a static context'
        cashDispensed = true;
//      BillDispenser.dispenseCash(10);
//      BillDispenser =(10);
//      BillDispenser.dispenseCash();
        mainMenu();
        break;
    case 2:
        accountWithdraw(20);
        amount = (20);
        CashDispenser.dispenseCash(amount); // same error here
        cashDispensed = true;
//      BillDispenser =(20);
//      BillDispenser.dispenseCash(20);
        mainMenu();
        break;
    case 3:
        accountWithdraw(50);
        amount = (50);
        CashDispenser.dispenseCash(amount); // same error here
        cashDispensed = true;
//      BillDispenser =(50);
//      BillDispenser.dispenseCash();
        mainMenu();
        break;
    case 4:
        accountWithdraw(100);
        amount = (100);
        CashDispenser.dispenseCash(amount); // same error here
        cashDispensed = true;
//      BillDispenser =(100);
//      BillDispenser.dispenseCash();
        mainMenu();
        break;
    case 5:
        accountWithdraw(200);
        amount = (200);
        CashDispenser.dispenseCash(amount); // Error ends here.
        cashDispensed = true;
//      BillDispenser =(200);
//      BillDispenser.dispenseCash();
        mainMenu();
        break;
    case 6:
        mainMenu();
        break;
}
}
public void accountWithdraw(int withdrawFunds){
    currentBal = currentBal -withdrawFunds;
    System.out.println("Please take your funds.");
}
public void depositFunds(){
    int addSelection;
    System.out.println("Amount to deposit: ");
    System.out.println("[1] - R20");
    System.out.println("[2] - R50");
    System.out.println("[3] - R100");
    System.out.println("[4] - R200");
    System.out.println("[5] - MAIN MENU");
    System.out.print("Please select your option now: ");
    addSelection =input.nextInt();
    switch (addSelection){
    case 1:
        accountAdd(20);
        mainMenu();
        break;
    case 2:
        accountAdd(50);
        mainMenu();
        break;
    case 3:
        accountAdd(100);
        mainMenu();
        break;
    case 4:
        accountAdd(200);
        mainMenu();
        break;
    case 5:
        mainMenu();
        break;
}
}
public void accountAdd (int depositFunds){
    currentBal = currentBal +depositFunds;
    System.out.println("Thank you.");
}
public static void main(String[] args) {

    new Main().mainMenu();  //creating the instance and calling the method
}
}

Recommended Answers

All 10 Replies

non-static method dispenseCash(int) cannot be referenced from a static context

Pretty much what is says.
dispenseCash is an instance method, and must be called using an instance of CashDispenser, but you try to call it just using the class name (as if it was a static method), so the compiler doesn't know which instance to use.

So How and where can I go about fixing this?

uhm, use cashDispenser instead of CashDispenser? And actually assign that variable a value, of course.

uhm, use cashDispenser instead of CashDispenser? And actually assign that variable a value, of course.

Thank you that really fixed the errors and now it can debug and once I've choosen the amount I want to withdraw it then gave me this error bellow. Is this error comes as a result that I was deburging this on a computer which doesn't have a Dispenser device or? Thanks again for your help.

Exception in thread "main" java.lang.NullPointerException
    at Main.withdrawFunds(Main.java:76)
    at Main.mainMenu(Main.java:33)
    at Main.main(Main.java:171)

Well thanks for the link but I didn't understand what I should do in my codes since you said you don't have the latest code well bellow is the new code which I changed the CashDispenser to cashDispenser and it indeed solved my first problem which was errors so now with this NullPointException I really don't know how can I assign this to a value. here is the latest snip code

switch (withdrawSelection){
    case 1:
        accountWithdraw(10);
        amount = 10;
        cashDispenser.dispenseCash(amount); //New code
        cashDispensed = true;
        mainMenu();
        break;

Thank you.

All your methods (except main) are (correctly) instance methods. You need to create an instance of CashDispenser to use those methods. You create an instance with (eg)
CashDispenser cashDispenser = new CashDispenser();
That creates an instance, and sets your cashDispenser variable to refer to it. Now you can call your methods.

Change line 10 from the OP

private CashDispenser cashDispenser;

and actually assign that variable a value other than null (which is the default for Objects declared in this manner).

:-) Thank you so much for solving this now I don't know whether I should worry about sorting the billRequired method or it will determine on it own when it has been installed on a device with a dispensing device.

Ok now my worry is the shouldn't in generate an exeption error since I'm debuging it from a computer which doen't have a dispenser device because now it just run smooth as the first time I didn't put the dispenser codes, shouldn't it produce an error stating that it didn't find any dispenser to dispense the choosen amount?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.