Skip to main content
se eliminaron 87 caracteres en el cuerpo
Origen Enlace
NeoChiri
  • 330
  • 1
  • 4
  • 15
public class Conexion {

private static Connection conexion=null;

/*Conectamos a la Base de Datos*/

public Conexion(){
    try{
        Class.forName("com.mysql.jdbc.Driver"); 
        conexion=DriverManager.getConnection("jdbc:mysql://localhost/chinook", "root", "");
    }catch(Exception excepcion){
    
     excepcion.printStackTrace();
   String }
}

/*DevolvemosalterIdArtista="ALTER laTABLE conexion*/

publicartist ConnectionMODIFY getConnectionCOLUMN ArtistId INT(11){
 AUTO_INCREMENT PRIMARY KEY";
 return conexion;
}

/*Cerramos la conexión a la Base deStatement Datos*/
sqlAlterArtista = conexion.createStatement();
public void desconectar      sqlAlterArtista.executeUpdate(alterIdArtista){;
    try{    
        conexionsqlAlterArtista.close();
    }catch(Exception excepcion){
        excepcion.printStackTrace();
    }
 }

}/*Devolvemos la conexion*/

Esto es de la clase Coordinador, que la uso como clase intermedia para realizar las uniones entre distintas clases (siguiendo el modelo MVC), os pongo la parte correspondiente de añadir el artista, ya que el resto solo va relacionado con las vistas:

 
public voidstatic addArtista(ArtistaVOConnection artistaVOabrir() {
    miLogica.validarRegistroArtistareturn conexion != null ? conexion : new Conexion(artistaVO);.conexion;
}

La clase ArtistaVO, donde realizo los get y set del campo artista de la base de datos:

 
public classstatic ArtistaVOboolean cerrar() {
 
  private int idArtista;try {
  private String nombreArtista;

  public int getIdArtistaconexion.close(){;
     return idArtista;
  }
return true;
  public void setIdArtista} catch(intException idArtistaexcepcion) {
     this.idArtista=idArtista;
  }

  public String getNombreArtistaexcepcion.printStackTrace(){;
      return nombreArtista;
 return }
false;
  public void setNombreArtista(String nombreArtista){}
   }  this.nombreArtista=nombreArtista;
  }
}

De} De la clase donde tengo los metodos de registro:

java.lang.NullPointerException
at controlador.Coordinador.addArtista(Coordinador.java:108)
at vista.VistaArtistasAdd.actionPerformed(VistaArtistasAdd.java:70)
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$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)

Añado esto porque es donde me muestra la excepcion que apunta NullPointerException

public void validarRegistroArtista(ArtistaVO artistaVO){
    ArtistaDAO artistaDAO;
    
    if(artistaVO.getIdArtista()>0){
        artistaDAO=new ArtistaDAO();
        artistaDAO.addArtista(artistaVO);
    }else{
        JOptionPane.showMessageDialog(null, "Debe de introducirse algún numero de ID", "Advertencia", JOptionPane.WARNING_MESSAGE);
    }
  }
public class Conexion {

Connection conexion=null;

/*Conectamos a la Base de Datos*/

public Conexion(){
    try{
        Class.forName("com.mysql.jdbc.Driver"); 
        conexion=DriverManager.getConnection("jdbc:mysql://localhost/chinook", "root", "");
    }catch(Exception excepcion){
        excepcion.printStackTrace();
    }
}

/*Devolvemos la conexion*/

public Connection getConnection(){
    return conexion;
}

/*Cerramos la conexión a la Base de Datos*/

public void desconectar(){
    try{
        conexion.close();
    }catch(Exception excepcion){
        excepcion.printStackTrace();
    }
 }

}

Esto es de la clase Coordinador, que la uso como clase intermedia para realizar las uniones entre distintas clases (siguiendo el modelo MVC), os pongo la parte correspondiente de añadir el artista, ya que el resto solo va relacionado con las vistas:

public void addArtista(ArtistaVO artistaVO){
    miLogica.validarRegistroArtista(artistaVO);
}

La clase ArtistaVO, donde realizo los get y set del campo artista de la base de datos:

public class ArtistaVO {
 
  private int idArtista;
  private String nombreArtista;

  public int getIdArtista(){
     return idArtista;
  }

  public void setIdArtista(int idArtista){
     this.idArtista=idArtista;
  }

  public String getNombreArtista(){
      return nombreArtista;
  }

  public void setNombreArtista(String nombreArtista){
     this.nombreArtista=nombreArtista;
  }
}

De la clase donde tengo los metodos de registro:

java.lang.NullPointerException
at controlador.Coordinador.addArtista(Coordinador.java:108)
at vista.VistaArtistasAdd.actionPerformed(VistaArtistasAdd.java:70)
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$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)
public class Conexion {

private static Connection conexion=null;

/*Conectamos a la Base de Datos*/

public Conexion(){
    try{
        Class.forName("com.mysql.jdbc.Driver"); 
        conexion=DriverManager.getConnection("jdbc:mysql://localhost/chinook", "root", "");
         
        String alterIdArtista="ALTER TABLE artist MODIFY COLUMN ArtistId INT(11) AUTO_INCREMENT PRIMARY KEY";
        Statement sqlAlterArtista = conexion.createStatement();
        sqlAlterArtista.executeUpdate(alterIdArtista);
        
        sqlAlterArtista.close();
    }catch(Exception excepcion){
        excepcion.printStackTrace();
    }
}

/*Devolvemos la conexion*/
 
public static Connection abrir() {
    return conexion != null ? conexion : new Conexion().conexion;
}
 
public static boolean cerrar() {
    try {
        conexion.close();
        return true;
    } catch(Exception excepcion) {
        excepcion.printStackTrace();
        return false;
    }
}    

} De la clase donde tengo los metodos de registro:

java.lang.NullPointerException
at controlador.Coordinador.addArtista(Coordinador.java:108)
at vista.VistaArtistasAdd.actionPerformed(VistaArtistasAdd.java:70)
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$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)

Añado esto porque es donde me muestra la excepcion que apunta NullPointerException

public void validarRegistroArtista(ArtistaVO artistaVO){
    ArtistaDAO artistaDAO;
    
    if(artistaVO.getIdArtista()>0){
        artistaDAO=new ArtistaDAO();
        artistaDAO.addArtista(artistaVO);
    }else{
        JOptionPane.showMessageDialog(null, "Debe de introducirse algún numero de ID", "Advertencia", JOptionPane.WARNING_MESSAGE);
    }
  }
Añadida excepcion
Origen Enlace
NeoChiri
  • 330
  • 1
  • 4
  • 15

Esta es la excepcion que me arroja

java.lang.NullPointerException
at controlador.Coordinador.addArtista(Coordinador.java:108)
at vista.VistaArtistasAdd.actionPerformed(VistaArtistasAdd.java:70)
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$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)

Esta es la excepcion que me arroja

java.lang.NullPointerException
at controlador.Coordinador.addArtista(Coordinador.java:108)
at vista.VistaArtistasAdd.actionPerformed(VistaArtistasAdd.java:70)
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$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)
Origen Enlace
NeoChiri
  • 330
  • 1
  • 4
  • 15

Me da error al intentar registrar una nueva persona (artista) en Java

Buenas a todos, estoy realizando un programa en Java en el cual debo registrar artistas, y también sus Albumes y Canciones (bueno, y también modificación y eliminación pero eso no viene ahora al caso).

Todo está conectado a una Base de Datos de MySql, el programa me solicita el nombre nada mas, os pongo los metodos que tengo para ello:

Esta es la clase Conexión, donde realizo la conexión a la BD:

public class Conexion {

Connection conexion=null;

/*Conectamos a la Base de Datos*/

public Conexion(){
    try{
        Class.forName("com.mysql.jdbc.Driver"); 
        conexion=DriverManager.getConnection("jdbc:mysql://localhost/chinook", "root", "");
    }catch(Exception excepcion){
        excepcion.printStackTrace();
    }
}

/*Devolvemos la conexion*/

public Connection getConnection(){
    return conexion;
}

/*Cerramos la conexión a la Base de Datos*/

public void desconectar(){
    try{
        conexion.close();
    }catch(Exception excepcion){
        excepcion.printStackTrace();
    }
 }

}

Esto es de la clase Coordinador, que la uso como clase intermedia para realizar las uniones entre distintas clases (siguiendo el modelo MVC), os pongo la parte correspondiente de añadir el artista, ya que el resto solo va relacionado con las vistas:

public void addArtista(ArtistaVO artistaVO){
    miLogica.validarRegistroArtista(artistaVO);
}

La clase ArtistaVO, donde realizo los get y set del campo artista de la base de datos:

public class ArtistaVO {

  private int idArtista;
  private String nombreArtista;

  public int getIdArtista(){
     return idArtista;
  }

  public void setIdArtista(int idArtista){
     this.idArtista=idArtista;
  }

  public String getNombreArtista(){
      return nombreArtista;
  }

  public void setNombreArtista(String nombreArtista){
     this.nombreArtista=nombreArtista;
  }
}

De la clase donde tengo los metodos de registro:

 public void addArtista(ArtistaVO artistaVO){
    
    Conexion conexion=new Conexion();
    
    try{
        //Insertamos los datos del Artista
        PreparedStatement  sqlAddArtist=conexion.getConnection().prepareStatement("INSERT into artist values(?,?)");
        ResultSet resultado=sqlAddArtist.executeQuery();
        while(resultado.next()){
            sqlAddArtist.setString(1, artistaVO.getNombreArtista());
            sqlAddArtist.setInt(2, getMaxId()+1);
        }
        JOptionPane.showMessageDialog(null, "Se ha añadido exitosamente","Información",JOptionPane.INFORMATION_MESSAGE);
        sqlAddArtist.close();
        conexion.desconectar();
        
    }catch(SQLException excepcion){
        System.out.println(excepcion.getMessage());
        JOptionPane.showMessageDialog(null,"No se registro", "Error", JOptionPane.ERROR_MESSAGE);
    }
}

Este es el metodo que uso para obtener la Id del Artista:

  public int getMaxId(){
    int id=0;
    Conexion conexion=new Conexion();
    
    try{
        Statement sqlMaxId=conexion.getConnection().createStatement();
        ResultSet resultado=sqlMaxId.executeQuery("SELECT max(ArtistId) from artist");
        
        if(resultado.next()){
            id=resultado.getInt(0);
        }
        
        conexion.desconectar();
        sqlMaxId.close();
    }catch(SQLException excepcion){
        System.out.println(excepcion.getMessage());
    }
    
    return id;
}

Y este es que está en la clase de la ventana de añadir el artista, para que al pulsar el botón se registre:

  public void actionPerformed(ActionEvent evento) {
    
    if(evento.getSource()==botonAñadir){
        
        try{
            ArtistaVO artistaVO=new ArtistaVO();
            artistaVO.setNombreArtista(campoTextoArtista.getText());
            
            miCoordinador.addArtista(artistaVO);
        }catch(Exception excepcion){
            JOptionPane.showMessageDialog(null, "Error al añadir Artista", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
}

El error que me salta es el de "Error al añadir Artista", asi que estoy totalmente perdido en donde puede estar el fallo, y seguramente sea una tontería pero estoy bloqueado.