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