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("");
}
}
}