Soy un programador novato bastante tratando de aprender los conceptos básicos del swing y la construcción de interfaz gráfica de usuario. tratando de construir una GUI de Java en el que tiene un balance final doble, inicial, y cuando se oprime un botón, añade un interés del 5% final doble a ella. Sigo recibiendo el error
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
y parece que no puede averiguar dónde me salió mal con esto. No tengo nulos y no tengo errores en eclipse. He buscado a este problema, pero parece variar caso por caso. Creo que el problema se produce cuando intento y el texto conjunto de la JLabel, pero incluso entonces no estoy seguro de cómo iba a arreglar eso.
aquí es mi código, he utilizado dos clases diferentes para mi interfaz gráfica de usuario, BalancePane.java y 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));
}
}
}