我在下面的代码中遇到了一些问题。它运行,但给了我一个错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at controlador.Arbitros_controlador.actionPerformed(Arbitros_controlador.java:125)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at javax.swing.JComboBox.selectWithKeyChar(Unknown Source)
at javax.swing.plaf.basic.BasicComboBoxUI$Handler.keyPressed(Unknown Source)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at javax.swing.JComboBox.processKeyEvent(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.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(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$200(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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)
我从中得知obj,还没有实例化,但是从if statements
的逻辑开始,它不应该抛出这个错误,并且一直在改变它我可以想到但它仍然会让我错误,请帮忙。我的代码是这样的:
这是视图类:
package vista;
import java.awt.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.*;
@SuppressWarnings("serial")
public class Arbitros_vista extends JPanel {
public JPanel panel_arbitroNuevo, panel_arbitroInfo, pnombre,
pnacionalidad, ppartidos, pposicion;
public JTable lista_arbitros;
public JButton partidosAsignados, crear_Arbitro, b3;
public JTextField nombre, nacionalidad, posicion;
public JLabel labelNombre, labelNacionalidad, labelPosicion;
public JComboBox<Integer> opcionesLista;
DateFormat formatoFecha = new SimpleDateFormat("dd-mm-yyyy");
JFormattedTextField entradaFecha = new JFormattedTextField(formatoFecha);
private JLabel amountOfGames;
// constructor creates all components inside the mainWindow tabs
public Arbitros_vista() {
this.setPanel_nuevo_Arbitro();
this.crearTablaDeArbitros();
this.setBackground(new Color(200, 200, 200));
this.add(this.panel_arbitroNuevo);
this.add(this.panel_arbitroInfo);
}
public void setPanel_nuevo_Arbitro() {
panel_arbitroNuevo = new JPanel();
panel_arbitroNuevo.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(new Color(150, 150, 150), 20),
"Nuevo Arbitro"));
panel_arbitroNuevo.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// creacion del label nommbre con su entrada de texto y ubicacion
// ====================================================================================================
labelNombre = new JLabel("Nombre:");
nombre = new JTextField(7);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5, 5, 5, 5);
panel_arbitroNuevo.add(labelNombre, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5, 5, 5, 5);
panel_arbitroNuevo.add(nombre, gbc);
// creacion del label nacionalidad con su entrada de texto y ubicacion
// ====================================================================================================
labelNacionalidad = new JLabel("Nacionalidad:");
nacionalidad = new JTextField(7);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5, 5, 5, 5);
panel_arbitroNuevo.add(labelNacionalidad, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5, 5, 5, 5);
panel_arbitroNuevo.add(nacionalidad, gbc);
// creacion de label posicion con su entrada de texto y ubicacion
// ====================================================================================================
labelPosicion = new JLabel("Posición:");
posicion = new JTextField(7);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5, 5, 5, 5);
panel_arbitroNuevo.add(labelPosicion, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5, 5, 5, 5);
panel_arbitroNuevo.add(posicion, gbc);
// creacion de DropDown list que contiene opciones para la cantidad de partidos asignados
// ====================================================================================================
amountOfGames = new JLabel("Partidos Asignados");
gbc.gridx = 3;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
panel_arbitroNuevo.add(amountOfGames, gbc);
Integer[] options = {0,1,2,3,4,5,6,7,8,9};
gbc.gridx = 3;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
opcionesLista = new JComboBox<Integer>(options);
opcionesLista.setSelectedIndex(0);
opcionesLista.setEditable(false);
panel_arbitroNuevo.add(opcionesLista, gbc);
// creacion de boton partidos asignados con su ubicacion
// =========================================================================================== =========
partidosAsignados = new JButton("Partidos Asignados");
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5, 5, 5, 5);
panel_arbitroNuevo.add(partidosAsignados, gbc);
// creacion de boton CREAR ARBITRO con su ubicacion
// ==============================================================
crear_Arbitro = new JButton("Crear");
gbc.gridx = 3;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5, 5, 5, 5);
panel_arbitroNuevo.add(crear_Arbitro, gbc);
}// end of setPanel_nuevo_Arbitro method
public void crearTablaDeArbitros() {
panel_arbitroInfo = new JPanel();
panel_arbitroInfo.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(new Color(0, 0, 0)),
"Información Arbitros"));
panel_arbitroInfo.setLayout(new GridBagLayout());
@SuppressWarnings("unused")
String x = "VS";
String[] cols = { "Nombre", "Nacionalidad", "Posicion",
"Partidos Asignados" };
String[][] rows = { { "esteban rincon", "colombiano",
"lateral izquierdo", " Ver " } };
lista_arbitros = new JTable(rows, cols);
JScrollPane scrol = new JScrollPane(lista_arbitros);
lista_arbitros.setFillsViewportHeight(false);
panel_arbitroInfo.add(scrol);
}
}
这将是后端课程:
package modelo;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
public class Arbitros_modelo {
private String NOMBRE, NACIONALIDAD, POSICION;
private int PARTIDOS_ASIG;
public String[] nom_columns_arbitros = { "Nombre", "Nacionalidad",
"Posicion", "Partidos Asignados" };
private ConectorBaseDatos conector;
/**
* Creates Object of type Arbitro that has information about the "Arbitro"
*
* @param nombre
* @param nacionalidad
* @param posicion
*/
public Arbitros_modelo(String nombre, String nacionalidad, String posicion,
int counter) {
NOMBRE = nombre;
NACIONALIDAD = nacionalidad;
POSICION = posicion;
PARTIDOS_ASIG = counter;
conector = new ConectorBaseDatos();
Object[] abr_nombre = { NOMBRE, NACIONALIDAD, POSICION }; // not really
// necessary
// ..
}
public String getNOMBRE() {
return NOMBRE;
}
public String getNACIONALIDAD() {
return NACIONALIDAD;
}
public String getPOSICION() {
return POSICION;
}
public int getPARTIDOS_ASIG() {
return PARTIDOS_ASIG;
}
public void intoARBITROS_TABLE() {
conector.connect_to_DB("fifa", "root", "");
conector.InsertIntoDB(
"INSERT INTO`arbitros`(NOMBRE,NACIONALIDAD,POSICION,PARTIDOS_ASIGNADOS)"
+ "VALUES('" + NOMBRE + "', '" + NACIONALIDAD + "', '"
+ POSICION + "', '" + PARTIDOS_ASIG + "')", "fifa",
"root", "");
JOptionPane.showMessageDialog(null, "nuevo arbitro creado !: \n"
+ NOMBRE);
}
}
这将是抛出错误的控制器:
package controlador;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventListener;
import javax.swing.JOptionPane;
import modelo.Arbitros_modelo;
import modelo.Arbitros_modelo_sub;
import vista.Arbitros_vista;
import vista.Arbitros_vista_sub;
public class Arbitros_controlador implements ActionListener {
private Arbitros_vista VISTA;
@SuppressWarnings("unused")
private Arbitros_modelo MODELO;
private Arbitros_vista_sub vistaSub;
@SuppressWarnings("unused")
private Arbitros_modelo_sub modeloSub;
@SuppressWarnings("unused")
private String inputOne;
@SuppressWarnings("unused")
private String inputTwo;
private String nombrE;
private String nacionalidaD;
private String posicioN;
private int counter;
/**
* Constructor creates a link between all GRAPHICAL INTERFACES AND BACK END|
* PROCESS
*
* @param vista
* @param vistasub
* @param modelo
* @param modeloSub
*/
public Arbitros_controlador(Arbitros_vista vista,
Arbitros_vista_sub vistasub, Arbitros_modelo modelo,
Arbitros_modelo_sub modeloSub) {
this.VISTA = vista;
this.vistaSub = vistasub;
this.MODELO = modelo;
this.modeloSub = modeloSub;
this.VISTA.partidosAsignados.addActionListener(this);
this.VISTA.crear_Arbitro.addActionListener(this);
this.VISTA.nombre.addActionListener(this);
this.VISTA.nacionalidad.addActionListener(this);
this.VISTA.posicion.addActionListener(this);
this.VISTA.opcionesLista.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
counter = (int) this.VISTA.opcionesLista.getSelectedItem();
if(e.getSource() == this.VISTA.crear_Arbitro){
nombrE = this.VISTA.nombre.getText();
nacionalidaD = this.VISTA.nacionalidad.getText();
posicioN = this.VISTA.posicion.getText();
this.MODELO = new Arbitros_modelo(nombrE, nacionalidaD, posicioN, counter); // CREATES ARBITRO
this.MODELO.intoARBITROS_TABLE();
}
// seeks PARTIDOS ASIGNADOS'S BUTTON
// ====================================================================================================================================
if (e.getSource() == VISTA.partidosAsignados) {
// input validation
// ====================================================================================================================================
if (nombrE.equals("") || nombrE.equals(null)
|| nacionalidaD.equals("") || nacionalidaD.equals(null)
|| posicioN.equals("") || posicioN.equals(null)
|| counter == 0) {
JOptionPane
.showMessageDialog(
null,
"Información del árbitro Incompleta.\n Debe llenar datos primarios"
+ " del árbitro antes de continuar con los partidos asignados.");
} else {
this.vistaSub = new Arbitros_vista_sub(nombrE);
this.vistaSub.equTxt.addActionListener(this);
this.vistaSub.equ2Txt.addActionListener(this);
this.vistaSub.ok.addActionListener(this);
this.modeloSub = new Arbitros_modelo_sub(nombrE);
// end of SUB- VISTA - IF-STATEMENT
}
// ====================================================================================================================================================================================
} else {
// IF MAIN BUTTON ISN'T CLICKED DO NOTHING
// ***********************************************************************
}// END OF first if statement seeks PARTIDOS ASIGNADOS'S BUTTON
// TODO need to check why its throwing a NullPointerException in next
// line everytime you pass from and to the JComboBox which gives the
// option to use X number to create X windows
// to insert into a ARBITRO_TABLE
// SEEKS OK BUTTON FOR SUB-VISTA JFRAME
// ============================================================================================================================================
if (e.getSource() == this.vistaSub.ok) {
inputOne = this.vistaSub.equTxt.getText();
inputTwo = this.vistaSub.equ2Txt.getText();
// INPUT VALIDATION FOR INNER SUB-VISTA JFRAME COMPONENTS
if (inputOne.equals("") || inputOne.equals(null)
|| inputOne.equals(" ") || inputTwo.equals("")
|| inputTwo.equals(null) || inputTwo.equals(" ")) {
JOptionPane.showMessageDialog(null, "Ingrese ambos equipos");
} else {
this.modeloSub.intoTable(inputOne, inputTwo);
this.vistaSub.closeFrame();
}// END OF INPUT VALIDATION FOR INNER SUB-VISTA JFRAME COMPONENTS
} else {
}
}
}
这就是一切都得到控制的地方:
import javax.swing.JFrame;
import modelo.Arbitros_modelo;
import modelo.Arbitros_modelo_sub;
import vista.Arbitros_vista;
import vista.Arbitros_vista_sub;
import controlador.Arbitros_controlador;
public class TESTER_ARBITROS {
public static void main(String[] args) {
Arbitros_vista vista = new Arbitros_vista();
Arbitros_vista_sub vistaSub = null;
Arbitros_modelo modelo = null;
Arbitros_modelo_sub modeloSub = null;
Arbitros_controlador arbitro = new Arbitros_controlador(vista, vistaSub, modelo, modeloSub);
javax.swing.JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(vista);
frame.pack();
frame.setVisible(true);
}
}
还有另一个类,即点击按钮时创建的JFrame
,但不要认为它是相关的
答案 0 :(得分:0)
at controlador.Arbitros_controlador.actionPerformed(Arbitros_controlador.java:125)
表示你在Arbitros_controlador.java的第125行有一个NullPointerException;这是行
if (e.getSource() == this.vistaSub.ok) {
所以我假设e.getSource()
返回null(这不太可能),或者this.vistaSub.ok
为null(这是我所期望的)。
答案 1 :(得分:0)
在您的主要方法中,您有:
Arbitros_vista_sub vistaSub = null;
...
Arbitros_controlador arbitro = new Arbitros_controlador(vista, vistaSub, modelo, modeloSub);
您确定要进入方法else
的{{1}}条件:
actionPerformed
如果没有,那么你知道为什么你有一个NullPointerException
要处理这样的NullPointerException,您可以在生成NPE的行之前添加这些行:
this.vistaSub = new Arbitros_vista_sub(nombrE);