Estoy tratando de hacer un ejercicio para aprender a usar WindowsBuilder y JFrame y cuando hago click en el botón para hacer los cálculos, me sale el error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at GUI$2.actionPerformed(GUI.java:109) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
El código entero es:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.awt.event.ActionEvent;
import javax.swing.border.TitledBorder;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.UIManager;
import java.awt.Color;
import javax.swing.JCheckBox;
public class GUI extends JFrame {
private JPanel contentPane;
private JTextField txtNumeroOrigen;
private JTextField txtNumeroDestino;
private JTextField txtDuracion;
private JTextField txtPrecio;
private JCheckBox checkboxProvincial;
private JCheckBox checkboxLocal;
private JCheckBox checkboxFranja1;
private JCheckBox checkboxFranja2;
private JCheckBox checkboxFranja3;
Centralita callcenter = new Centralita();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI frame = new GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GUI() {
setResizable(false);
setTitle("Call Center");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 190);
contentPane = new JPanel();
contentPane.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Calcular Llamadas", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblTipo = new JLabel("Tipo:");
lblTipo.setFont(new Font("Tahoma", Font.BOLD, 11));
lblTipo.setBounds(6, 30, 46, 14);
contentPane.add(lblTipo);
JLabel lblFraja = new JLabel("Fraja:");
lblFraja.setFont(new Font("Tahoma", Font.BOLD, 11));
lblFraja.setBounds(6, 107, 46, 14);
contentPane.add(lblFraja);
JLabel lblNmeroOrigen = new JLabel("N\u00FAmero Origen");
lblNmeroOrigen.setBounds(128, 30, 108, 14);
contentPane.add(lblNmeroOrigen);
JLabel lblNmeroDestino = new JLabel("N\u00FAmero Destino");
lblNmeroDestino.setBounds(128, 55, 108, 14);
contentPane.add(lblNmeroDestino);
JLabel lblDuracion = new JLabel("Duraci\u00F3n");
lblDuracion.setBounds(128, 81, 108, 14);
contentPane.add(lblDuracion);
txtNumeroOrigen = new JTextField();
txtNumeroOrigen.setBounds(246, 30, 130, 20);
contentPane.add(txtNumeroOrigen);
txtNumeroOrigen.setColumns(10);
txtNumeroDestino = new JTextField();
txtNumeroDestino.setBounds(246, 55, 130, 20);
contentPane.add(txtNumeroDestino);
txtNumeroDestino.setColumns(10);
txtDuracion = new JTextField();
txtDuracion.setBounds(246, 81, 130, 20);
contentPane.add(txtDuracion);
txtDuracion.setColumns(10);
JButton btnCalcular = new JButton("Calcular");
btnCalcular.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Si es Llamada Local
if (checkboxLocal.isSelected())
{
LlamadaLocal callLocal = new LlamadaLocal (txtNumeroOrigen.getText(), txtNumeroDestino.getText(), Integer.parseInt(txtDuracion.getText()));
callcenter.registrarLlamada(callLocal);
refrescarPrecio(callcenter.getTotalFacturado());
}
// Si es Llamada Provincial y Franja 1
else if (checkboxProvincial.isSelected() && checkboxFranja1.isSelected())
{
LlamadaProvincial callProvincial = new LlamadaProvincial (txtNumeroOrigen.getText(), txtNumeroDestino.getText(), Integer.parseInt(txtDuracion.getText()), 1);
callcenter.registrarLlamada(callProvincial);
refrescarPrecio(callcenter.getTotalFacturado());
}
// Si es Llamada Provincial y Franja 2
else if (checkboxProvincial.isSelected() && checkboxFranja2.isSelected())
{
LlamadaProvincial callProvincial = new LlamadaProvincial (txtNumeroOrigen.getText(), txtNumeroDestino.getText(), Integer.parseInt(txtDuracion.getText()), 2);
callcenter.registrarLlamada(callProvincial);
refrescarPrecio(callcenter.getTotalFacturado());
}
// Si es Llamada Provincial y Franja 3
else if (checkboxProvincial.isSelected() && checkboxFranja3.isSelected())
{
LlamadaProvincial callProvincial = new LlamadaProvincial (txtNumeroOrigen.getText(), txtNumeroDestino.getText(), Integer.parseInt(txtDuracion.getText()), 3);
callcenter.registrarLlamada(callProvincial);
refrescarPrecio(callcenter.getTotalFacturado());
}
// Si nada esta seleccionado
else
JOptionPane.showMessageDialog(null, "Debe seleccionar el tipo de llamada y la franja.");
}
});
btnCalcular.setBounds(147, 128, 89, 23);
contentPane.add(btnCalcular);
JButton btnLimpiar = new JButton("Limpiar");
btnLimpiar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
txtNumeroOrigen.setText("");
txtNumeroDestino.setText("");
txtDuracion.setText("");
txtPrecio.setText("");
checkboxProvincial.setSelected(false);
checkboxLocal.setSelected(false);
checkboxFranja1.setSelected(false);
checkboxFranja2.setSelected(false);
checkboxFranja3.setSelected(false);
}
});
btnLimpiar.setBounds(246, 128, 89, 23);
contentPane.add(btnLimpiar);
txtPrecio = new JTextField();
txtPrecio.setForeground(Color.RED);
txtPrecio.setEditable(false);
txtPrecio.setBounds(345, 129, 86, 20);
contentPane.add(txtPrecio);
txtPrecio.setColumns(10);
JLabel lblPrecio = new JLabel("Precio:");
lblPrecio.setFont(new Font("Tahoma", Font.BOLD, 11));
lblPrecio.setBounds(385, 107, 46, 14);
contentPane.add(lblPrecio);
JCheckBox checkboxProvincial = new JCheckBox("Provincial");
checkboxProvincial.setBounds(6, 51, 97, 23);
contentPane.add(checkboxProvincial);
JCheckBox checkboxLocal = new JCheckBox("Local");
checkboxLocal.setBounds(6, 77, 97, 23);
contentPane.add(checkboxLocal);
JCheckBox checkboxFranja1 = new JCheckBox("1");
checkboxFranja1.setBounds(6, 128, 39, 23);
contentPane.add(checkboxFranja1);
JCheckBox checkboxFranja2 = new JCheckBox("2");
checkboxFranja2.setBounds(47, 128, 39, 23);
contentPane.add(checkboxFranja2);
JCheckBox checkboxFranja3 = new JCheckBox("3");
checkboxFranja3.setBounds(88, 128, 46, 23);
contentPane.add(checkboxFranja3);
}
public void refrescarPrecio(double precio)
{
DecimalFormat df = (DecimalFormat)NumberFormat.getInstance();
df.applyPattern("$ ###,###.##");
String Precio = "";
Precio = df.format(Precio);
txtPrecio.setText(Precio);
}
}
¿A qué puntero se refiere el error? No entiendo cuál es el problema.
GUI.java:109
. ¿Cual es la sentencia en la línea 109?