我试过修理但是我做不到。它在进入方法mainMenu时崩溃并出现此错误:
Exception in thread "main" java.lang.NullPointerException
at bank.VersionBankMachine.mainMenu(VersionBankMachine.java:137)
at bank.VersionBankMachine.main(VersionBankMachine.java:120)
调试器说错误在行选项= input.nextFloat();和mainMenu();
我必须明天完成这项任务,因为我的老师正在检查我的代码的功能,所以如果它不起作用那将是一个问题。否则感谢您的帮助!
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)
;
}