简体   繁体   中英

Getting a null point exception in Java code for Bank App

Hello everyone I am trying to test my code but when for my application I try to sabotage the code by writing cc or ss when the code asks for checking or savings account, I get a null pointer exception.This doesnt happen when I try to do the same by writing ww or dd for withdraw or deposit. We have to make sure that the program is fool proof. Here is the part of the code where the error is occuring.

static Scanner sc = new Scanner(System.in);


    public static void validator(){

        boolean isvalid=false;
            while(isvalid==false)
{
        System.out.println("Withdraw or deposit? (w/d):");

    if ((sc.hasNext("d"))||(sc.hasNext("w"))||(sc.hasNext("W"))||(sc.hasNext("D"))){
        transaction = sc.next();
        isvalid=true;
    }
    else {
        sc.nextLine();
        System.out.println("Invalid Input! Type again");
        continue;
    }

        System.out.println("Checking or saving? (c/s):");

        if((sc.hasNext("c"))||(sc.hasNext("C"))||(sc.hasNext("s"))||(sc.hasNext("S"))){
            accountType = sc.next();
            isvalid=true;
        }
        else{
            sc.nextLine();
            System.out.println("Invalid Input! Type again");
            continue;
            }

        System.out.println("Amount? :");

        if(sc.hasNextDouble()){
        creditDebit = sc.nextDouble();
        isvalid=true;
        }
        else{
            sc.nextLine();
            System.out.println("Invalid Input! Type again");
            continue;
        }

        if(creditDebit<1000){
            isvalid=true;
        }
        else{
            sc.nextLine();
            System.out.println("Input must be lower than $1000");
            continue;
        }

        }
}       


    public static void main(String[] args) {

          String choice="y";
          CheckingAccount c= new CheckingAccount();
          SavingsAccount s=new SavingsAccount();
          BankDisplay account1= new BankDisplay();

            System.out.println("Checking Amount:"+account1.getChecking());
            System.out.println("Saving Amount:"+account1.getSaving());    
 do{
     try{

        validator();
        if(accountType.isEmpty()){
        accountType=null;
        }

        if (transaction.equalsIgnoreCase("w") && accountType.equalsIgnoreCase("c")){
            c.withdraw(creditDebit);
        }
        else if (transaction.equalsIgnoreCase("w") && accountType.equalsIgnoreCase("s")){
            s.withdraw(creditDebit);
        }
        else if (transaction.equalsIgnoreCase("d") && accountType.equalsIgnoreCase("c")){
            c.deposit(creditDebit);
        }
        else if (transaction.equalsIgnoreCase("d") && accountType.equalsIgnoreCase("s")){
            s.deposit(creditDebit);
        }


        System.out.println("Continue? (y/n): ");
        choice=sc.next();   
     }
     catch(InputMismatchException e){
            System.out.println("Invalid Input ! Please try again:");    
            sc.next();
            continue;
     }
 }
    while(!choice.equalsIgnoreCase("n"));

Every time you call hasNext you're consuming the next token. Your (sc.hasNext("d"))||(sc.hasNext("w"))||(sc.hasNext("W"))||(sc.hasNext("D")) will eat some tokens from the file. The number of tokens depends on how quickly, if at all, the condition becomes true.

This is probably not what you intend.

Capture the next token and test it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM