基本 Java GUI 利息计算器中线程 "AWT-EventQueue-0" java.lang.NullPointerException 中的异常

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in basic Java GUI interest calculator

我是一名漂亮的新手程序员,正在尝试学习 Swing 和 GUI 构建的基础知识。尝试构建一个 Java Gui,其中它需要一个最终的双倍初始余额,并在按下按钮时向其添加最终双倍 5% 的利息。我一直收到错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at interest.Balance$ButtonListener.actionPerformed(Balance.java:30)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6401)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90

而且似乎无法弄清楚我哪里出了问题。我没有空值,我在 eclipse 中也没有错误。我已经搜索了这个问题,但它似乎因情况而异。我认为当我尝试将文本设置为 JLabel 时会出现问题,但即便如此我也不确定我将如何解决该问题。

这是我的代码,我为我的 GUI 使用了两个不同的 类,BalancePane.java 和 Balance.java

import javax.swing.JFrame;

public class BalancePane {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        Balance panel = new Balance();

        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);    
        frame.getContentPane().add(panel);  
        frame.setSize(300, 500);    
        frame.pack();
        frame.setVisible(true);
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Balance extends JPanel {
    private JTextField balanceTextField;
    private final double INITIAL_BALANCE = 1000;
    private final double INTEREST_RATE = 5; 

    public Balance() {
        JButton interestButton = new JButton("Add interest");
        JTextField balanceTextField = new JTextField(10);

        interestButton.addActionListener(new ButtonListener());

        add(interestButton);
        add(balanceTextField);

        balanceTextField.setText(Double.toString(INITIAL_BALANCE));
    }

    private class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {

            double balance = INITIAL_BALANCE * (INTEREST_RATE)/100;
            balanceTextField.setText(Double.toString(balance));
        }
    }
}

public class Balance extends JPanel {
    private JTextField balanceTextField;
    // ...

balanceTextField是一个字段,最初是null.

    public Balance() {
        // ...
        JTextField balanceTextField = new JTextField(10);

糟糕。 balanceTextField 是局部变量。永远不会设置同名字段。

        // ...
            balanceTextField.setText(Double.toString(balance));

这将取消引用从未分配的 balanceTextField 字段。

您正在声明 2 个 JTextField 具有相同名称的变量,初始化其中一个并使用另一个:

要修复它只需将 JTextField balanceTextField = new JTextField(10); 更改为 balanceTextField = new JTextField(10);