我正在玩JAVA,想要模拟一个ATM系统。我有一个BankMember类(int accountNumber,int pin,int balance,String name)和一个使用HashMap的UserDatabase 密钥将是accountNumber,我想将BankMember存储在HashMap中。
我有一个UserInterface接受帐号和PIN码,并尝试登录UserDatabase。
我能够毫无问题地将OBJ存储到HashMap中,但是当我尝试从BankMember中调用方法时,我得到nullPointerExceptions。
Exception in thread "main" java.lang.NullPointerException
at atm.UserDatabase.validateLogin(UserDatabase.java:65)
at atm.TheBank.login(TheBank.java:46)
at atm.UserInterface.validateLogin(UserInterface.java:38)
at atm.UserInterface.login(UserInterface.java:31)
at atm.ATM.main(ATM.java:24)
Java Result: 1
我知道我的BankMember没有正确运行方法调用,但我不知道如何解决这个问题。我不应该为此使用HashMap吗?
这是我的userDatabase类
package atm;
import java.util.HashMap;
/**
*
* @author Hybrid
*/
public class UserDatabase {
BankMember bankMember;
private int currentAccount; //when a user logs in, the currentAccount is stored for quick access to the EU's key
private HashMap<Integer, BankMember> accountNumberAndMember; //account number, BankMember
public UserDatabase() {
bankMember = new BankMember();
currentAccount = 0;
accountNumberAndMember = new HashMap<Integer, BankMember>();
}
public boolean validateAccountNumberAvailable(int accountNumber) {
boolean validate = true;
if (accountNumberAndMember.containsKey(accountNumber)) {
System.out.println("Account # already in use: "+ accountNumber);
validate = false;
}
return validate;
}
private boolean validateAccountExists(int accountNumber) {
boolean validate = true;
if (accountNumberAndMember.containsKey(accountNumber)) {
validate = true;
}
return validate;
}
/**
*
* @param accountNumber validate the account number is not already in the database.
* @param bankMember if the account number is not already used, then the bankMember is stored within the database
* @return true is bankMember stored, false if not
*/
public boolean setUpNewUser(int accountNumber, int pin, int balance, String name) {
boolean validate = true;
if(!accountNumberAndMember.containsKey(accountNumber)) {
bankMember = new BankMember(accountNumber, pin, balance, name);
System.out.println("Member stored in database with key: " + accountNumber);
accountNumberAndMember.put(accountNumber, bankMember);
System.out.println(accountNumberAndMember.get(accountNumber).getName()+ " was stored in the map as a new user.");
} else {
System.out.println("This account has already been created\nAccount Number:"+accountNumber);
validate = false;
}
return validate;
}
/**
*
* @param accountNumber this is the key for accountNumberAndMember map
* @param pin this is tested within the BankMember to verify it is truly them attempting to login.
* @return true is login is successful. False if not with proper error message.
*/
public boolean validateLogin(int accountNumber, int pin) {
System.out.println("::::::::::::LOGIN SYSTEM::::::::::::");
boolean validate = validateAccountExists(accountNumber);
if(validate) {
System.out.println("\n\n" + accountNumberAndMember.get(accountNumber).getName()); // THIS FAILS
if(accountNumber == accountNumberAndMember.get(accountNumber).getAccountNumber()) { //if account number is within the map
if(pin == (accountNumberAndMember.get(accountNumber).getPin())) {
System.out.println("Login successful");
currentAccount = accountNumberAndMember.get(accountNumber).getAccountNumber();
} else {
System.out.println("Invalid Pin: " + pin);
validate = false;
}
} else {
System.out.println("Invalid Account Number: "+ accountNumber);
validate = false;
}
} else {
System.out.println("The account number does not exist: " + accountNumber);
}
return validate;
}
//Resetting Map key
public void logOff() {
currentAccount = 0;
}
}
答案 0 :(得分:2)
无论如何,您的validateAccountExists
方法始终会返回true
。简单地
return accountNumberAndMember.containsKey(accountNumber);