Ayuda con una NPE
Publicado por System.out.println("Me llamo Carlos") (43 intervenciones) el 15/07/2020 20:51:43
Hola buenas, estoy haciendo un ejercicio de swing, pero me sale una Null pointer exeption, la verdad es que no detecto que variable tiene valor nullo, ayudarme por favor...
Os dejo todas las clases para ver si detectais el error:
En el método ActionPerformed, cuando descomento el if, el NPE, desaparece.
Os dejo todas las clases para ver si detectais el error:
1
2
3
4
5
6
7
8
9
10
11
package creador_mensajes;
public class MainCreador {
public static void main(String[] args) {
FrameCreador f = new FrameCreador();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package creador_mensajes;
import javax.swing.JFrame;
public class FrameCreador extends JFrame {
public FrameCreador() {
setTitle("Creador de mensajes");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(480, 200, 500, 500);
setResizable(false);
add(new PanelPrinicipal());
setVisible(true);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package creador_mensajes;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
public class PanelPrinicipal extends JPanel {
JButton crearMensajeButton;
public PanelPrinicipal() {
JPanel panelBotton = new JPanel();
//TODO El boton ponerle la accion
crearMensajeButton = new JButton(new AccionMensaje());
// ------------------------------------------------------
setLayout(new BorderLayout());
// ------------------------------------------------------
panelBotton.add(crearMensajeButton);
add(new PanelCentro(), BorderLayout.CENTER);
add(panelBotton, BorderLayout.SOUTH);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package creador_mensajes;
import java.awt.GridLayout;
import javax.swing.JPanel;
public class PanelCentro extends JPanel {
private static PanelJRadio Tipo;
private static PanelJRadio TipoDeMensaje;
private static PanelJRadio Mensaje;
private static PanelJRadio Confirmar;
private static PanelJRadio Opcion;
private static PanelJRadio Entrada;
public PanelCentro() {
setLayout(new GridLayout(2, 3));
String[] tipo = { "Mensaje", "Confirmar", "Opcion", "Entrada" };
String[] tipoDeMensaje = { "ERROR_MESSAGE", "INFORMATION_MESSAGE", "WARNING_MESSAGE", "QUESTION_MESSAGE",
"PLAIN_MESSAGE" };
String[] mensaje = { "Cadena", "Icono", "Componente", "Otros", "Object[]" };
String[] confirmar = { "DEFAULT_OPTION", "YES_NO_OPTION", "YES_NO_CANCEL_OPTION", "0K_CANCEL_OPTION" };
String[] opcion = { "String[]", "Icon[]", "Object[]" };
String[] entrada = { "Campo de texto", "Combo" };
Tipo = new PanelJRadio("Tipo", tipo);
TipoDeMensaje = new PanelJRadio("Tipo de Mensaje", tipoDeMensaje);
Mensaje = new PanelJRadio("Mensaje", mensaje);
Confirmar = new PanelJRadio("Confirmar", confirmar);
Opcion = new PanelJRadio("Opcion", opcion);
Entrada = new PanelJRadio("Entrada", entrada);
add(Tipo);
add(TipoDeMensaje);
add(Mensaje);
add(Confirmar);
add(Opcion);
add(Entrada);
}
public static PanelJRadio getTipo() {
return Tipo;
}
public static PanelJRadio getTipoDeMensaje() {
return TipoDeMensaje;
}
public static PanelJRadio getMensaje() {
return Mensaje;
}
public static PanelJRadio getConfirmar() {
return Confirmar;
}
public static PanelJRadio getOpcion() {
return Opcion;
}
public static PanelJRadio getEntrada() {
return Entrada;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package creador_mensajes;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
public class PanelJRadio extends JPanel {
private ButtonGroup group;
public PanelJRadio(String titulo, String[] opciones) {
setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), titulo));
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
group = new ButtonGroup();
for (int i = 0; i < opciones.length; i++) {
JCheckBox box = new JCheckBox(opciones[i]);
add(box);
group.add(box);
box.setSelected(i == 0);
}
}
public String dameElBotonSelecionado () {
String i = group.getSelection().getActionCommand();
return i;
}
}
En el método ActionPerformed, cuando descomento el if, el NPE, desaparece.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package creador_mensajes;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
public class AccionMensaje extends AbstractAction {
public AccionMensaje() {
putValue(AbstractAction.NAME, "Crear mensaje");
putValue(AbstractAction.SHORT_DESCRIPTION,
"Un boton que al clicarlo te crea un mensaje con los parametros seleccionados");
}
@Override
public void actionPerformed(ActionEvent e) {
PanelJRadio tipo = PanelCentro.getTipo();
PanelJRadio tipoDeMensaje = PanelCentro.getTipoDeMensaje();
PanelJRadio mensaje = PanelCentro.getMensaje();
PanelJRadio confirmar = PanelCentro.getConfirmar();
PanelJRadio opcion = PanelCentro.getOpcion ();
PanelJRadio entrada = PanelCentro.getEntrada();
if (tipo.dameElBotonSelecionado().equals("Mensaje")) {
//Cuando descomento el if no me sale la "NPE"
System.out.println("FUNCIONA!");
}
//TODO:Poner todas las opciones posibles...
}
}
Valora esta pregunta


0