JFrame银行帐户空指针异常

时间:2016-10-18 02:03:28

标签: java swing nullpointerexception

我正在网上完成一些基本的java教程,过去几个小时我一直在银行账户程序中遇到Null Pointer Exception。通常当我遇到这些时,我可以很快解决这个错误,但在这种情况下,错误总是指向按钮的行,我称之为方法(存款或取款)。我不确定究竟是什么问题,因为我已经初始化了我在两个类中使用的所有变量,但我仍然遇到这个异常。出了什么问题?

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

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

}

0 个答案:

没有答案