Search code examples
javaexceptionnumber-formatting

Confusing number format exceptions


I'm getting a whole slew of exceptions that seem to stem from my parse double since their info is: Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String another likely suspect is

account1.withdrawl(checkingBal, changeValue) ;

which is convinced it's an unreported exception despite my try catch block in the method being called:

public double withdrawl(double currentBal, double withdrawlAmount) throws InsufficientFunds{
    int i =0 ;
    double overDraw = 0 ;
    try{
    //verifies that input was numeric and evenly divisible by 20
  if(withdrawlAmount % 20 == 0){
    if(i>4){
        if(currentBal>1.5){
        currentBal = currentBal-1.5 ;
        }else{
        JOptionPane.showMessageDialog(null, "Insufficient funds");
        overDraw = currentBal-1.5 ;
        throw new InsufficientFunds(overDraw); 
        }
    }
    //stops withdrawl for insufficient funds
    if(currentBal<withdrawlAmount) {
        JOptionPane.showMessageDialog(null, "Insufficient funds");
        overDraw = currentBal-withdrawlAmount ;
        throw new InsufficientFunds(overDraw);
    }else {
        currentBal=currentBal-withdrawlAmount ;
    }
    i++ ;
  }else {
  JOptionPane.showMessageDialog(null, "Please enter a withdarl amount that is divisible by 20");
  }
    }catch (InsufficientFunds ex){
        ex.getAmount();
    }
  return currentBal ;
 }




public class ATM extends JFrame implements ActionListener {
JButton withdraw, deposit, transfer, balance ;
JTextField input ;
boolean checkingActive = true ;
public double checkingBal = 0 ;
public double savingsBal = 0 ;

Main class:

ATM(){
withdraw = new JButton ("Withdraw") ;
deposit = new JButton ("Deposit") ;
transfer = new JButton ("Transfer") ;
balance = new JButton ("Balance") ;
input = new JTextField (12) ;
input.setText("0") ;
input.setEditable(true) ;

JRadioButton checking = new JRadioButton("Checking", true) ;;
JRadioButton savings = new JRadioButton("Savings") ;
this.add(checking);
this.add(savings);
ButtonGroup acctSelector = new ButtonGroup();
acctSelector.add(checking);
acctSelector.add(savings);


add(withdraw);
add(deposit);
add(transfer);
add(balance);
add(input);

//assigns buttons to logic gorup
ButtonGroup accountChoice = new ButtonGroup();
accountChoice.add(checking) ;
accountChoice.add(savings) ;

input = new JTextField(6) ;

//assigns listeners to buttons
withdraw.addActionListener(this) ;
deposit.addActionListener(this) ;
transfer.addActionListener(this) ;
balance.addActionListener(this) ;

setSize(500,400) ;
setLayout(new FlowLayout()) ;
setTitle("MoneyBank ATM") ;
}

//acceses account changes
public void actionPerformed(ActionEvent e){
 double changeValue = Double.parseDouble(input.getText().trim());
 Account account1 = new Account() ;
 if(checkingActive==true){
    if(e.getSource()== withdraw){
      account1.withdrawl(checkingBal, changeValue) ;
    } else if(e.getSource()== deposit) {
       account1.deposit(checkingBal, changeValue) ;
    } else if(e.getSource()== transfer){
       account1.transfer(checkingBal, changeValue) ;
    } else {
       account1.balance(checkingBal) ;
    }
 } else {
    if(e.getSource()== withdraw){
      account1.withdrawl(savingsBal, changeValue) ;
    } else if(e.getSource()== deposit) {
       account1.deposit(savingsBal, changeValue) ;
    } else if(e.getSource()== transfer){
       account1.transfer(savingsBal, changeValue) ;
    } else {
       account1.balance(savingsBal) ;  
    }
 }
JOptionPane.showMessageDialog(null, "Current balances are: \n Savings: " + savingsBal + "\n Checking: "+ checkingBal);
}

//constructs atm object/GUI
public static void main(String[] args){
ATM account=new ATM();
account.setVisible(true);
account.setLocation(200,200);
account.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public boolean pickAccount(boolean k) {
   checkingActive = k ;
   return checkingActive ;
}

}

Solution

  • You're creating 2 different JTextField instances for the variable input.

    The first:

    input = new JTextField(12);
    input.setText("0");
    input.setEditable(true);
    

    The second:

    input = new JTextField(6);
    

    So, when you write a number in the field and send the value to process, what you are receiving is an empty value "". And the end your application tries to generate a Double from that empty causing:

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    

    You can comment or delete the second instance and your application will work.