0

I am working through some basic java tutorials online, and I have been stuck for the past few hours on a Null Pointer Exception in a Bank Account program. Usually when I encounter these, I can pretty quickly resolve the bug, but in this case the error always points to the line of the button where I call a method (deposit or withdraw). I'm not sure what precisely the issue is, since I have initialized all the variables I am using in both classes, but I am still encountering this exception. Any ideas what is going wrong?

BankAccount.java

import javax.swing.*;

class BankAccount {
private int accountBalance;
private JTextField stateTextField, balanceTextField;

public BankAccount(JTextField stateField, JTextField balanceField) {
    stateTextField = stateField;
    balanceTextField = balanceField;
    accountBalance = 0;
}

public int getBalance() {
    return accountBalance;
}

public void setBalance(int balance) {
    accountBalance = balance;
}

public void withdraw(int amount) {
    accountBalance = accountBalance - amount;
    displayState(stateTextField);
    displayBalance(balanceTextField);
}

public void deposit(int amount) {
    accountBalance = accountBalance + amount;
    displayState(stateTextField);
    displayBalance(balanceTextField);
}
public void displayState(JTextField field) {
    if (accountBalance>=0) {
        field.setText("Ok");
    } else {
        field.setText("Overdrawn");
    }
}

public void displayBalance(JTextField field) {
    int balance = accountBalance;
    field.setText(Integer.toString(balance));
}
}

And the interface where I use BankAccount.java:

BankAccountInterface.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BankAccountInterface extends JFrame implements ActionListener {
private JLabel balanceLabel, stateLabel, withdrawLabel, depositLabel;
private JTextField balanceField, stateField, withdrawField, depositField;
private JButton depositButton, withdrawButton, clearButton;
private BankAccount account;

public static void main(String[] args) {
    BankAccountInterface demo = new BankAccountInterface();
    demo.setSize(750, 400);
    demo.setLocationRelativeTo(null);
    demo.createGUI();
    demo.setVisible(true);
}

public void createGUI() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container window = getContentPane();
    window.setLayout(new FlowLayout() );

    stateLabel = new JLabel("State:");
    window.add(stateLabel);

    stateField = new JTextField(10);
    stateField.setEditable(true);
    window.add(stateField);

    balanceLabel = new JLabel("Balance:");
    window.add(balanceLabel);

    balanceField = new JTextField(10);
    balanceField.setEditable(true);
    window.add(balanceField);

    depositLabel = new JLabel("Deposit:");
    window.add(depositLabel);

    depositField = new JTextField(10);
    depositField.setEditable(true);
    window.add(depositField);

    withdrawLabel = new JLabel("Withdraw:");
    window.add(withdrawLabel);

    withdrawField= new JTextField(10);
    withdrawField.setEditable(true);
    window.add(withdrawField);

    depositButton = new JButton("Deposit");
    window.add(depositButton);
    depositButton.addActionListener(this);

    withdrawButton = new JButton("Withdraw");
    window.add(withdrawButton);
    withdrawButton.addActionListener(this);

    clearButton = new JButton("Clear");
    window.add(clearButton);
    clearButton.addActionListener(this);

    BankAccount account = new BankAccount(stateField, balanceField);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == depositButton) {
        int depositVal = Integer.parseInt(depositField.getText() );
        account.deposit(depositVal);
    } else if (e.getSource() == withdrawButton) {
        int withdrawVal = Integer.parseInt(withdrawField.getText() );
        account.withdraw(withdrawVal);
    } else {
        depositField.setText("");
        withdrawField.setText("");
        stateField.setText("");
        balanceField.setText("");
    }
}

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 3
    The heuristic for NullPointerExceptions is almost always the same. **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. In the future, please search on the subject before posting, since this is too common a problem to post yet another NPE question. – Hovercraft Full Of Eels Oct 18 '16 at 02:05
  • Once you find out which variable is at fault (and a debugger can help) it will be obvious why (look up "variable shadowing" for more on your problem). – Hovercraft Full Of Eels Oct 18 '16 at 02:06
  • `it will be obvious why (look up "variable shadowing" for more on your problem` -Then maybe this question should be closed as a duplicated of something that actually talks about "variable shadowing"??? For example: http://stackoverflow.com/questions/17757537/action-listener-for-jbutton-isnt-working/17757565#17757565 – camickr Oct 18 '16 at 03:09
  • Thanks for the help! The error was being thrown on the method 'account.deposit(depositVal);', and I was having trouble figuring it out. After taking my eyes off the problem for a couple hours, I realized the error was related to variable shadowing as you mentioned. Thanks for giving me that exact terminology, though, I hope to avoid that problem in the future. – John Oct 19 '16 at 01:56

0 Answers0