0

I'm having some problems with the code below. It runs, but gives me a this error:

 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)

I take it from this that the obj, was not instantiated yet, and but going from the logic of the if statements then it shouldn't throw this error, and have been changing it in any way I can think of but it still throws me the error, please help. my code is this:

this is view class:

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);

}


}

this would be the back end class:

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);

}

}

and this would be the controller where the error is thrown:

  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 {

    }



}

}

and this is where everything is controlled:

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);

        }
}

there is another class which is the JFrame that is created when the button is clicked, but don't think its relevant

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3363537
  • 111
  • 1
  • 1
  • 8
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Apr 18 '14 at 12:11
  • That's a bit general, but correct. I'd rather refer to [how to read a stacktrace](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors). – ThaMe90 Apr 18 '14 at 12:19
  • @AndrewThompson, thanks for the tip I'll be doing this for a next occasion. – user3363537 Apr 18 '14 at 13:00

2 Answers2

0
at controlador.Arbitros_controlador.actionPerformed(Arbitros_controlador.java:125)

Indicates you have a NullPointerException at line 125 in Arbitros_controlador.java; which is the line

if (e.getSource() == this.vistaSub.ok) {

So I assume e.getSource() returns null (which is unlikely), or this.vistaSub.ok is null (which is what I expect).

ThaMe90
  • 4,196
  • 5
  • 39
  • 64
0

In your main method, you have :

Arbitros_vista_sub vistaSub = null;
...
Arbitros_controlador arbitro = new   Arbitros_controlador(vista, vistaSub, modelo, modeloSub);

are you sure you enter into the else condition of your method actionPerformed where you do :

this.vistaSub = new Arbitros_vista_sub(nombrE);

If not, then you know why you have a NullPointerException

To handle such a NullPointerException, you could add this lines before the one that produce the NPE:

if (this.vistaSub == null) {
    this.vistaSub = new Arbitros_vista_sub(nombrE);
}
fluminis
  • 3,575
  • 4
  • 34
  • 47
  • thanks, how can i handle this error if i want the obj to be created on this button clicked ? is there a way to handle this error ? – user3363537 Apr 18 '14 at 13:14