我是一位相当新手的程序员,她试图学习swing和GUI构建的基础知识。尝试构建一个Java 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$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.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$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.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
而且似乎无法弄清楚我在哪里出错了。我没有null,并且在eclipse中没有错误。我已经搜索了此问题,但情况视情况而异。我认为当我尝试将JLabel的文本设置为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
字段。