NullPointer when opening a window from another

0

I have two windows that I show you below:

vistaAlbumesBuscar

public class VistaAlbumesBuscar extends JFrame implements ActionListener{

/**
 * Componentes de la ventana
 */
private Coordinador miCoordinador;
private JTextField campoTextoArtista, campoTextoAlbum;
private JLabel etiquetaArtista, etiquetaAlbum, etiquetaArtista2, etiquetaAlbum2, etiquetaCanciones;
private JSeparator separador;
private JButton botonModificar, botonEliminar, botonEliminar2, botonAdd;
private JList listaCanciones;
private DefaultListModel modeloLista;
private JScrollPane scrollListadoCanciones;
private JComboBox elegirAlbum, elegirArtista;
private DefaultComboBoxModel modeloCombo;
private Generador miGenerador;

/**
 * Iniciamos la ventana
 */
public VistaAlbumesBuscar() {
    iniciarVentana();
}

/**
 * Contenido de la ventana.
 */
private void iniciarVentana() {

    /*Propiedades Frame*/
    setTitle("Albums: Search");
    setBounds(100, 100, 353, 437);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    getContentPane().setLayout(null);
    setLocationRelativeTo(null);
    setResizable(false);

    /*Etiqueta Artista*/
    etiquetaArtista = new JLabel("Artist:");
    etiquetaArtista.setFont(new Font("Tahoma", Font.PLAIN, 14));
    etiquetaArtista.setBounds(10, 11, 46, 14);
    getContentPane().add(etiquetaArtista);

    /*Etiqueta de Album*/
    etiquetaAlbum = new JLabel("Album:");
    etiquetaAlbum.setFont(new Font("Tahoma", Font.PLAIN, 14));
    etiquetaAlbum.setBounds(10, 36, 46, 14);
    getContentPane().add(etiquetaAlbum);

    /*Separador*/
    separador = new JSeparator();
    separador.setBounds(0, 76, 337, 2);
    getContentPane().add(separador);

    /*Etiqueta Artista II*/
    etiquetaArtista2 = new JLabel("Artist:");
    etiquetaArtista2.setFont(new Font("Tahoma", Font.PLAIN, 14));
    etiquetaArtista2.setBounds(10, 89, 46, 14);
    getContentPane().add(etiquetaArtista2);

    /*Campo texto Artista*/
    campoTextoArtista = new JTextField();
    campoTextoArtista.setBounds(62, 89, 231, 20);
    getContentPane().add(campoTextoArtista);
    campoTextoArtista.setColumns(10);
    campoTextoArtista.setEditable(false);

    /*Etiqueta Album II*/
    etiquetaAlbum2 = new JLabel("Album:");
    etiquetaAlbum2.setFont(new Font("Tahoma", Font.PLAIN, 14));
    etiquetaAlbum2.setBounds(10, 121, 46, 14);
    getContentPane().add(etiquetaAlbum2);

    /*Campo texto Album*/
    campoTextoAlbum = new JTextField();
    campoTextoAlbum.setBounds(62, 120, 231, 20);
    getContentPane().add(campoTextoAlbum);
    campoTextoAlbum.setColumns(10);

    /*Boton Modificar*/
    botonModificar = new JButton("Modify");
    botonModificar.setBounds(62, 151, 89, 23);
    botonModificar.addActionListener(this);
    getContentPane().add(botonModificar);

    /*Boton Eliminar*/
    botonEliminar = new JButton("Delete");
    botonEliminar.setBounds(161, 151, 89, 23);
    botonEliminar.addActionListener(this);
    getContentPane().add(botonEliminar);

    /*Etiqueta Canciones*/
    etiquetaCanciones = new JLabel("Tracks:");
    etiquetaCanciones.setFont(new Font("Tahoma", Font.PLAIN, 14));
    etiquetaCanciones.setBounds(10, 206, 76, 14);
    getContentPane().add(etiquetaCanciones);

    /*Lista de Canciones*/
    scrollListadoCanciones=new JScrollPane();
    listaCanciones = new JList();
    modeloLista=new DefaultListModel();
    scrollListadoCanciones.setBounds(10, 226, 210, 163);
    getContentPane().add(scrollListadoCanciones);
    scrollListadoCanciones.setViewportView(listaCanciones);

    /*Boton Eliminar II*/
    botonEliminar2 = new JButton("Delete");
    botonEliminar2.setBounds(226, 366, 101, 23);
    getContentPane().add(botonEliminar2);

    /*Boton Añadir*/
    botonAdd = new JButton("Add");
    botonAdd.setBounds(226, 332, 101, 23);
    botonAdd.addActionListener(this);
    getContentPane().add(botonAdd);

    /*Combo Artista*/
    elegirArtista = new JComboBox();
    modeloCombo = new DefaultComboBoxModel();
    miGenerador=new Generador();
    elegirArtista.addActionListener(this);
    elegirArtista.setBounds(66, 10, 227, 20);
    getContentPane().add(elegirArtista);
    miGenerador.llenarComboArtistas(modeloCombo, elegirArtista);


    /*Combo Albumes*/
    elegirAlbum = new JComboBox();
    elegirAlbum.setBounds(66, 35, 227, 20);
    elegirAlbum.addActionListener(this);
    getContentPane().add(elegirAlbum);  
}

public void setCoordinador(Coordinador miCoordinador){
    this.miCoordinador=miCoordinador;
}

@Override
public void actionPerformed(ActionEvent evento) {
    if(evento.getSource()==elegirArtista){
        if(elegirArtista.getSelectedIndex()>0){
            miGenerador.llenarComboAlbumes(modeloCombo, elegirAlbum, elegirArtista);
            campoTextoArtista.setText((String)elegirArtista.getSelectedItem());

        }
    }

    if(evento.getSource()==elegirAlbum){
        campoTextoAlbum.setText((String)elegirAlbum.getSelectedItem());
    }

    if(elegirAlbum.getSelectedIndex()>=0){
        AlbumVO albumVO=new AlbumVO();
        AlbumDAO albumDAO=new AlbumDAO();

        albumVO.setNombreAlbum(campoTextoAlbum.getText());
        miGenerador.llenarListaCanciones(albumVO, modeloLista, listaCanciones, albumDAO);
    }

    if(evento.getSource()==botonModificar){
        try{
            AlbumVO albumVO=new AlbumVO();
            AlbumDAO albumDAO=new AlbumDAO();

            String nuevoNombreAlbum=campoTextoAlbum.getText();
            albumVO.setNombreAlbum(elegirAlbum.getSelectedItem().toString());
            albumDAO.modificarAlbum(albumVO, nuevoNombreAlbum);

        }catch(Exception excepcion){
            excepcion.printStackTrace();
        }
    }

    if(evento.getSource()==botonEliminar){
        int respuesta=JOptionPane.showConfirmDialog(this, "¿Estás seguro de que deseas eliminar el Album?","Confirmación",JOptionPane.YES_NO_OPTION);
        if(respuesta==JOptionPane.YES_OPTION){
            AlbumDAO albumDAO=new AlbumDAO();
            AlbumVO albumVO=new AlbumVO();
            ArtistaDAO artistaDAO=new ArtistaDAO();
            ArtistaVO artistaVO=new ArtistaVO();

            artistaVO.setArtistName(campoTextoArtista.getText());
            albumVO.setNombreAlbum(campoTextoAlbum.getText());
            albumDAO.eliminarAlbum(artistaDAO, artistaVO, albumDAO, albumVO);
        }
    }

    if(evento.getSource()==botonAdd){
        VistaCancionesAdd ventanaCancionesAdd=new VistaCancionesAdd();
        AlbumVO albumVO=new AlbumVO();
        AlbumDAO albumDAO=new AlbumDAO();

        albumVO.setNombreAlbum(campoTextoAlbum.getText());
        ventanaCancionesAdd.abrirVentanaCanciones(campoTextoArtista, campoTextoAlbum, albumDAO, albumVO, modeloLista, listaCanciones);
        ventanaCancionesAdd.setVisible(true);
    }
}

public void abrirVentana(JTextField campoTextoArtista2, DefaultListModel modeloLista, JList listadoAlbumes){

    miGenerador.llenarComboArtistas(modeloCombo, elegirArtista);
    miGenerador.llenarComboAlbumes(modeloCombo, elegirAlbum, elegirArtista);
    elegirArtista.setSelectedItem(campoTextoArtista2.getText());
    elegirAlbum.setSelectedItem(listadoAlbumes.getSelectedValue());
}

}

And it's like this:

And this is the other:

VistaCancionesAdd

public class VistaCancionesAdd extends JFrame{

private Coordinador miCoordinador;
private JTextField campoTextoArtistaCancion, campoTextoAlbumCancion, campoTextoCancion;
private JLabel etiquetaArtista, etiquetaAlbum, etiquetaCanciones, etiquetaCancion;
private JSeparator separador, separador2;
private JList listaCanciones;
private DefaultListModel modeloLista;
private JScrollPane scrollListadoCanciones;
private JButton botonAdd, botonAdd2;
private Generador miGenerador;

/**
 * Iniciar la Ventana
 */
public VistaCancionesAdd() {
    iniciarVentana();
}

/**
 * Contenido de la ventana
 */
private void iniciarVentana() {

    /*Propiedades del Frame*/
    setTitle("Canciones: A\u00F1adir");
    setBounds(100, 100, 354, 374);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    getContentPane().setLayout(null);

    /*Etiqueta Artista*/
    etiquetaArtista = new JLabel("Artista:");
    etiquetaArtista.setFont(new Font("Tahoma", Font.PLAIN, 14));
    etiquetaArtista.setBounds(10, 11, 46, 14);
    getContentPane().add(etiquetaArtista);

    /*Campo Texto Artista (No editable)*/
    campoTextoArtistaCancion = new JTextField();
    campoTextoArtistaCancion.setBounds(66, 10, 251, 20);
    getContentPane().add(campoTextoArtistaCancion);
    campoTextoArtistaCancion.setEditable(false);
    campoTextoArtistaCancion.setColumns(10);

    /*Etiqueta Album*/
    etiquetaAlbum = new JLabel("Album:");
    etiquetaAlbum.setFont(new Font("Tahoma", Font.PLAIN, 14));
    etiquetaAlbum.setBounds(10, 43, 46, 14);
    getContentPane().add(etiquetaAlbum);

    /*Campo texto Album (No editable)*/
    campoTextoAlbumCancion = new JTextField();
    campoTextoAlbumCancion.setBounds(66, 41, 251, 20);
    getContentPane().add(campoTextoAlbumCancion);
    campoTextoAlbumCancion.setEditable(false);
    campoTextoAlbumCancion.setColumns(10);

    /*Separador 1*/
    separador = new JSeparator();
    separador.setBounds(0, 70, 338, 2);
    getContentPane().add(separador);

    /*Etiqueta Canciones*/
    etiquetaCanciones = new JLabel("Canciones:");
    etiquetaCanciones.setFont(new Font("Tahoma", Font.PLAIN, 14));
    etiquetaCanciones.setBounds(10, 83, 74, 14);
    getContentPane().add(etiquetaCanciones);

    /*Listado Canciones*/
    scrollListadoCanciones=new JScrollPane();
    listaCanciones = new JList();
    modeloLista=new DefaultListModel();
    scrollListadoCanciones.setBounds(10, 103, 216, 138);
    getContentPane().add(scrollListadoCanciones);
    scrollListadoCanciones.setViewportView(listaCanciones);

    /*Boton Añadir*/
    botonAdd = new JButton("Modificar");
    botonAdd.setBounds(239, 218, 89, 23);
    getContentPane().add(botonAdd);

    /*Separador 2*/
    separador2 = new JSeparator();
    separador2.setBounds(0, 262, 338, 2);
    getContentPane().add(separador2);

    /*Etiqueta Cancion*/
    etiquetaCancion = new JLabel("Canci\u00F3n:");
    etiquetaCancion.setFont(new Font("Tahoma", Font.PLAIN, 14));
    etiquetaCancion.setBounds(10, 293, 62, 14);
    getContentPane().add(etiquetaCancion);

    /*Campo Texto Cancion*/
    campoTextoCancion = new JTextField();
    campoTextoCancion.setBounds(82, 292, 144, 20);
    getContentPane().add(campoTextoCancion);
    campoTextoCancion.setColumns(10);

    /*Boton Añadir 2*/
    botonAdd2 = new JButton("A\u00F1adir");
    botonAdd2.setBounds(239, 291, 89, 23);
    getContentPane().add(botonAdd2);
}
public void setCoordinador(Coordinador miCoordinador){
    this.miCoordinador=miCoordinador;
}

public void abrirVentanaCanciones(JTextField campoTextoArtista, JTextField campoTextoAlbum, AlbumDAO albumDAO, AlbumVO albumVO, DefaultListModel modeloLista, JList listaCanciones){
    campoTextoArtistaCancion.setText(campoTextoArtista.getText());
    campoTextoAlbumCancion.setText(campoTextoAlbum.getText());
    miGenerador.llenarListaCanciones2(albumVO, modeloLista, listaCanciones, albumDAO);
}
}

And it's like this:

As of vistaAlbumesBuscar I try to open VistaCancionesAdd for it I use this method that is inside VistaCancionesAdd

 public void abrirVentanaCanciones(JTextField campoTextoArtista, JTextField campoTextoAlbum, AlbumDAO albumDAO, AlbumVO albumVO, DefaultListModel modeloLista, JList listaCanciones){
    campoTextoArtistaCancion.setText(campoTextoArtista.getText());
    campoTextoAlbumCancion.setText(campoTextoAlbum.getText());
    miGenerador.llenarListaCanciones(albumVO, modeloLista, listaCanciones, albumDAO);
   }

And I call it that from VistaAlbumesBuscar :

 if(evento.getSource()==botonAdd){
        VistaCancionesAdd ventanaCancionesAdd=new VistaCancionesAdd();
        AlbumVO albumVO=new AlbumVO();
        AlbumDAO albumDAO=new AlbumDAO();

        albumVO.setNombreAlbum(campoTextoAlbum.getText());
        ventanaCancionesAdd.abrirVentanaCanciones(campoTextoArtista, campoTextoAlbum, albumDAO, albumVO, modeloLista, listaCanciones);
        ventanaCancionesAdd.setVisible(true);
      }

This is the llenarListaCanciones() method

 public void llenarListaCanciones(AlbumVO albumVO, DefaultListModel modeloLista,JList listaCanciones, AlbumDAO albumDAO){

    Conexion conexion=new Conexion();
    //modeloLista.removeAllElements();
    try{
        String consultaSql="select name from track where albumid=?";
        PreparedStatement sqlNombreCancion=conexion.getConexion().prepareStatement(consultaSql);
        sqlNombreCancion.setInt(1, albumDAO.getIdAlbum(albumVO));
        ResultSet resultado=sqlNombreCancion.executeQuery();
        while(resultado.next()){
            modeloLista.addElement(resultado.getString("name"));
            listaCanciones.setModel(modeloLista);
        }
        sqlNombreCancion.close();
        conexion.cerrarConexion();
    }catch(SQLException excepcion){
        excepcion.printStackTrace();
    }
   }

Well, in VistaAlbumesBuscar when I hit the Add button (as it is in the image) this exception jumps me:

 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at vista.VistaCancionesAdd.abrirVentanaCanciones(VistaCancionesAdd.java:119)
at vista.VistaAlbumesBuscar.actionPerformed(VistaAlbumesBuscar.java:215)
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)

Nobody needs to explain to me what the exception NullPointerException is, since it means that it throws a null value (I do not want to offend anyone with this, but it happened to me that they answered what it meant and that did not solve my problem), if not that I know that the problem is in llenarListaCanciones() but I can not identify which part, so I ask for help.

Thank you very much.

#ACTUALIZATION 1

This is line 119:

miGenerador.llenarListaCanciones2(albumVO, modeloLista, listaCanciones, albumDAO);

    
asked by NeoChiri 20.05.2016 в 21:29
source

1 answer

2

I advise you to see well the trace of the error and try to locate what may be failing.

According to the trace the error is thrown here: vista.VistaCancionesAdd.abrirVentanaCanciones(VistaCancionesAdd.java:119) then locate in your class VistaCancionesAdd line 119 and see what is the object you are trying to access and / or modify. Because what is happening is that, you are trying to modify or access something, which at that moment has not been initialized, instantiated, etc.

I can not know because I simply do not see the line numbers of the code and you have not placed the imports (to copy your classes with the exact number of lines to evaluate the trace), but analyzing it a bit. You have the method abrirVentanaCanciones that according to the trace is from where the problem originates.

Your method looks like this

public void abrirVentanaCanciones(JTextField campoTextoArtista, JTextField    campoTextoAlbum, AlbumDAO albumDAO, AlbumVO albumVO, DefaultListModel modeloLista, JList listaCanciones){
    campoTextoArtistaCancion.setText(campoTextoArtista.getText());
    campoTextoAlbumCancion.setText(campoTextoAlbum.getText());
    miGenerador.llenarListaCanciones(albumVO, modeloLista, listaCanciones, albumDAO);
}

Before doing anything in this method, verify that everything is different from null to say something

if(AlbumDAO != null && modeloLista != null && listaCanciones != null) {
    //logica actual
}

That way you avoid this kind of inconvenience.

Please provide the code of the indicated line to extend the answer, based on it.

    
answered by 20.05.2016 / 21:54
source