1

I have tried fixing it but I can't. It crashes when it enters the method mainMenu with this error:

Exception in thread "main" java.lang.NullPointerException
at bank.VersionBankMachine.mainMenu(VersionBankMachine.java:137)
at bank.VersionBankMachine.main(VersionBankMachine.java:120)

The debugger says the error is in the line option = input.nextFloat(); and mainMenu();

I have to get this done tomorrow as my teacher is reviewing my code for functionality, so if it does't work that's going to be a problem. Otherwise thanks for the help!

    import java.util.Scanner;
    import java.util.concurrent.TimeUnit;

    public class VersionBankMachine {
    public static float bal, option, deposit, withdraw;
    public static String name;
    public static Scanner input;

    public static void main(String[] args) throws InterruptedException {
        // This is so input will not get a error that it has never been used
        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);
        // Float for the Balance

        // Log in Screen
        System.out.println("|----------------------------------|");
        System.out.println("|       Welcome to Maze Bank       |");
        System.out.println("|    Serving America since 1966    |");
        System.out.println("|        Press [3] to login        |");
        System.out.println("|       Press [2] to register      |");
        System.out.println("|        Press [1] to quit         |");
        System.out.println("|----------------------------------|");
        int setting = 0;
        try {
            setting = input.nextInt();
        } catch (java.util.InputMismatchException e) {
            System.out.println("Invalid option, please restart the program");
        }
        // Switch Statement for the menu
        switch (setting) {
        // Case 1 if the user would like to quit the program
        case 1:

            while (setting == 1) {
                System.out.println("Thank you for choosing Maze Bank!");
                System.out.println("We hope to see you again!");
                System.exit(0);
            }
            // Registration screen
        case 2:
            int creditcard = 0;

            while (setting == 2) {

                System.out.println("Please enter a 6-digit number");
                try {
                    creditcard = input.nextInt();
                } catch (java.util.InputMismatchException e) {
                    System.out.print("Invalid bank account number, please restart the program");
                    System.exit(0);
                }

                // If they put a bank account number of 000000 it is invalid

                if (creditcard > 100000 && creditcard < 999999)
                    System.out.println("Please enter your last name");
                String name1 = input.next();
                System.out.println("The name for your account number is " + name1);
                TimeUnit.SECONDS.sleep(1);
                System.out.println("Your account number is " + creditcard);
                TimeUnit.SECONDS.sleep(1);
                break;
            }

            // Log in screen
        case 3:
            if (setting == 3) {
                System.out.println("Please enter your credit card number");
                int creditcard1;
                try {
                    creditcard1 = input.nextInt();
                } catch (Exception e) {
                    System.out.print("Invalid bank account number, please restart the program");
                    return;
                }

                // If they put a bank account number of 000000 it is invalid
                if (creditcard1 <= 100000) {
                    System.out.print("Invalid bank account number, please try again");
                    creditcard1 = input.nextInt();
                }
                while (creditcard1 >= 999999) {
                    System.out.print("Invalid bank account number, please try again");
                    creditcard1 = input.nextInt();

                }
            }

            // Logging in screen
            else

                TimeUnit.SECONDS.sleep(1);
            System.out.println("Checking your details");
            TimeUnit.SECONDS.sleep(1);
            System.out.println("Logging in...");
            TimeUnit.SECONDS.sleep(1);
            System.out.println("Approved");
            TimeUnit.SECONDS.sleep(1);
            System.out.println("Please enter your sur-name");

            name = input.next();

            System.out.println("Loading please standby...");
            TimeUnit.SECONDS.sleep(3);
            int option = 0;
            mainMenu();
        }
        // Do statement for the menu
    }

    public static void mainMenu() throws InterruptedException {

        do {
            System.out.println("            Welcome Mr. " + name);
            System.out.println("|----------------------------------------|");
            System.out.println("|               Main Menu                |");
            System.out.println("|    Thank you for choosing Maze Bank    |");
            System.out.println("      Your current balance is $" + bal);
            System.out.println("|       Press [4] to deposit money       |");
            System.out.println("|         Press [5] to withdraw money    |");
            System.out.println("|       Press [6] to log off             |");
            System.out.println("|----------------------------------------|");
            option = input.nextFloat();

            if (option == 4) {

                deposit();

            }
            if (option == 5)

                withdraw();
        } while (option != 6);
        if (option == 6) {
            logoff();

        }
        while (option != 6)
            ;

    }
Omkar
  • 11
  • 4

0 Answers0