私はJavaでアーティスト、アルバム、トラックを登録できるソフトウェアを作成しています。Javaでアーティスト(人物)を追加するときのエラー
すべてがMySQL DB
に接続されています。ソフトウェアにはアーティスト名のみが必要です。私が作成した方法を示します。申し訳ありませんが、名前の多くはスペイン語ですが、この は私の学校のためであり、あまりにも多くの英語の単語を使用することはできません。これはクラスCoordinador
、このクラスは異なるクラス間で労働組合を作るために使用されている
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();
}
}
}
(以下のモデル:私はあなたが
これは、DBへの接続を行うクラスConexión
であることを理解することができると思いますMVC)、私はあなたにアーティストを追加することから部品を貼り付け、他の人がちょうどビューに関連しています
public void addArtista(ArtistaVO artistaVO){
miLogica.validarRegistroArtista(artistaVO);
}
私が取得し、sの作成クラスArtistaVO
アーティストの他アーティストを登録する
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;
}
}
の方法:
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);
}
}
この方法は、アーティストIDを作成するために使用されている:
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;
}
これはアーティストレジスタウィンドウに属しボタンをクリックした後:
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);
}
}
}
エラーはError al añadir Artista
(アーティストを追加中のエラー)ですが、どこに障害があるのか分かりません。
これは私が得る例外です:
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)
これはメソッドvalidarRegistroArtista
次のとおりです。
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);
}
}
エラーは 'Coordinador.java:108'行にあり、' NullPointerException' ...原因ですか?私たちは 'miLogica.validarRegistroArtista(artistaVO);のコードを見るべきですが、' artistaVO == null' ....でもベットできますhttp://es.stackoverflow.com –
ちょうどメソッドを追加しました – NeoChiri
@ナディールは正しいと思われます。手順を踏んで、私たちに知らせてください。重複した質問もチェックしてください。NPEはローカライズする必要があります....そして最後に**スペインのコーポレートライフの先生に教えてください。 ... ** –