Salut ! je suis débutant en javant et jaimerais pouvoir ajouter des enregistrement dans mabase de donnée.
Mon problème c'est que lorsque j'exécute mon code jai des érreurs
Erreurs:Exception
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at devlog.AjoutP.ajouterClient(AjoutP.java:129)
at devlog.AjoutP.actionPerformed(AjoutP.java:89)
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)Merci de m'apporter votre aideVoici mon code en dessous: package log;
import java.awt.*;
import javax.swing.*;
import javax.swing.JComboBox;
import java.awt.event.*;
import java.sql.*;
import java.io.*;
public class AjoutP extends JFrame implements ActionListener {
JTextField txtrefP,txtNProd,txtqt,txtNomFour,txtcat,txtadr,txtTel,txtmont,txtmoy;
JButton btnAjout,btnEffacer,btnFermer;
public static void main(String[]args)
{
AjoutP frame=new AjoutP();
}
public AjoutP()
{
Container c=this.getContentPane();
c.setLayout(new GridLayout(3,5));
JPanel panelcentre=new JPanel(new GridLayout(9,2));
JPanel panelbas=new JPanel(new FlowLayout());
JLabel titre=new JLabel("",SwingConstants.RIGHT);
titre.setForeground(Color.red);
titre.setFont(new Font("TimesRoman",Font.ITALIC,25));
titre.setText("FIRST APPLICATION JAVA CORP.");
c.add(titre);
//zone de saisie pour le nom l'adresse et le telephone
txtrefP=new JTextField();
txtNProd=new JTextField();
txtqt=new JTextField();
txtNomFour=new JTextField();
txtcat=new JTextField();
txtadr=new JTextField();
txtTel=new JTextField();
txtmont=new JTextField();
Object[] pai= {"Chèque bancaire", "Espèce","Carte bleu", "Chèques"};
JComboBox txtmoy = new JComboBox(pai);
//ajout des etiquettes et des zones de saisie au panel
panelcentre.add(new JLabel("REFERENCE PRODUIT :",SwingConstants.RIGHT));
panelcentre.add(txtrefP);
panelcentre.add(new JLabel("NOM PRODUIT :",SwingConstants.RIGHT));
panelcentre.add(txtNProd);
panelcentre.add(new JLabel("QUANTIE :",SwingConstants.RIGHT));
panelcentre.add(txtqt);
panelcentre.add(new JLabel("CATEGORIE :",SwingConstants.RIGHT));
panelcentre.add(txtcat);
panelcentre.add(new JLabel("NOM FOURNISSEUR :",SwingConstants.RIGHT));
panelcentre.add(txtNomFour);
panelcentre.add(new JLabel("ADRESSE :",SwingConstants.RIGHT));
panelcentre.add(txtadr);
panelcentre.add(new JLabel("TELEPHONE :",SwingConstants.RIGHT));
panelcentre.add(txtTel);
panelcentre.add(new JLabel("MONTANT :",SwingConstants.RIGHT));
panelcentre.add(txtmont);
panelcentre.add(new JLabel("MOYEN :",SwingConstants.RIGHT));
panelcentre.add(txtmoy);
c.add(panelcentre);
//creer et ajoutter des boutons au panel de bas
btnAjout=new JButton("Ajouter");
btnEffacer=new JButton("Effacer");
btnFermer=new JButton("Fermer");
panelbas.add(btnAjout);
panelbas.add(btnEffacer);
panelbas.add(btnFermer);
c.add(panelbas);
//enregistrer le frame comme auditeur de bouton
btnAjout.addActionListener(this);
btnEffacer.addActionListener(this);
btnFermer.addActionListener(this);
this.setSize(550,600);
this.setTitle("AJOUT DE PRODUIT");
this.setVisible(true);
//creer une classe anonyme pour gerer la fermeture de la fenetre
this.addWindowListener(new WindowAdapter()
{
public void windoClosing(WindowEvent ev)
{
fermerFormulaire();
}
}
);
}//fin du constructeur
public void actionPerformed(ActionEvent e)
{
//déterminer si on cliqué sur un bouton
if(e.getSource()==btnAjout)
{
ajouterClient();
}
if(e.getSource()==btnEffacer)
{effacerFormulaire();}
if(e.getSource()==btnFermer)
{fermerFormulaire();}
}
//ajout
private void ajouterClient()
{
String url = "com.mysql.jdbc.Driver";
Connection con = null;
try {
String refProd,NomProd,Quant,categorie,NomFour,adressLivr,Tel,montCommande,moyPaiement;
refProd=txtrefP.getText();
NomProd=txtNProd.getText();
Quant=txtqt.getText();
NomFour=txtNomFour.getText();
categorie=txtcat.getText();
NomFour=txtNomFour.getText();
adressLivr=txtadr.getText();
Tel=txtTel.getText();
montCommande=txtmont.getText();
moyPaiement=txtmoy.getSelectedText();
System.out.println(refProd+" "+NomProd+" "+Quant+""+categorie+""+NomFour+" "+adressLivr+""+Tel+""+montCommande+""+moyPaiement);
Class.forName(url);
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/gestion","root","");
//insertion
Statement s = con.createStatement();
String str = "INSERT INTO produi (refProd, NomProd ,Quant ,categorie ,NomFour, adressLivr, Tel,montCommande,moyPaiement) VALUES ('"+txtrefP.getText()+"','"+txtNProd.getText()+"','"+txtqt.getText()+"','"+txtcat.getText()+"','"+txtNomFour.getText()+"','"+txtadr.getText()+"','"+txtTel.getText()+"','"+txtmont.getText()+"','"+txtmoy.getSelectedText()+"')" ;
s.executeUpdate(str);
System.out.println(str+" ligne insérée");
JOptionPane.showMessageDialog(this,"ajouté");
con.close() ;
}catch(Exception e) {System.out.println("Exception");}
finally {
try {con.close();}
catch(SQLException e) {e.printStackTrace();}
}
effacerFormulaire();
}
//effacer
private void effacerFormulaire()
{
txtrefP.setText("");
txtNProd.setText("");
txtqt.setText("");
txtNomFour.setText("");
txtcat.setText("");
txtNomFour.setText("");
txtadr.setText("");
txtTel.setText("");
txtmont.setText("");
}
//fermer
public void fermerFormulaire()
{
this.setVisible(false);
System.exit(0);
}
}