IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JDBC Java Discussion :

Exception in thread "main" java.lang.NullPointerException


Sujet :

JDBC Java

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2011
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2011
    Messages : 17
    Points : 13
    Points
    13
    Par défaut Exception in thread "main" java.lang.NullPointerException
    salut tou le monde !! mon probleme c'est loreque j'execute mon programme j'ai ca

    Exception in thread "main" java.lang.NullPointerException
    at Application.fermeture(Application.java:436)
    at Application.main(Application.java:448)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Fournisseur.actionPerformed(Application.java:114)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)

    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

    Process completed.


    voici mon code !!

    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    /////////////////////////////////////////onglet fournisseur//////////////////////////////////////////////////////////////
    class Fournisseur extends JPanel implements ActionListener {
    public Connection con = null;
    public Statement st= null;
    public ResultSet rs= null;



    JLabel l1 = new JLabel(" Id-F");
    JLabel l2 = new JLabel(" Nom-F");
    JLabel l3 = new JLabel(" Prenom-F");
    JLabel l4 = new JLabel(" Adresse");
    JLabel l5 = new JLabel(" N° Telephone");

    JLabel l6 = new JLabel("");
    JLabel l7 = new JLabel("");
    JLabel l8 = new JLabel("");

    JTextField t1 = new JTextField();
    JTextField t2 = new JTextField();
    JTextField t3 = new JTextField();
    JTextField t4 = new JTextField();
    JTextField t5 = new JTextField();


    JButton bt1 = new JButton("Ajouter");
    JButton bt2 = new JButton("Supprimer");

    public JPanel p1 = new JPanel();


    public Fournisseur() {




    p1.setLayout(new GridLayout(5, 3, 3,3));
    p1.add(l1);
    p1.add(t1);
    p1.add(bt1);
    p1.add(l2);
    p1.add(t2);
    p1.add(bt2);
    p1.add(l3);
    p1.add(t3);
    p1.add(l6);
    p1.add(l4);
    p1.add(t4);
    p1.add(l7);
    p1.add(l5);
    p1.add(t5);
    p1.add(l8);

    bt1.addActionListener(this);
    bt2.addActionListener(this);

    this.add(p1);
    this.setSize(400,400);

    }

    /////////////// recuperer le text dans le textfield ///////////////////////////////////////////////////////////////////

    public String gett1() {

    return t1.getText();
    }
    //////
    public String gett2() {

    return t2.getText();
    }
    //////
    public String gett3() {

    return t3.getText();
    }
    //////
    public String gett4() {

    return t4.getText();
    }
    //////
    public String gett5() {

    return t5.getText();
    }
    //////////////////////////////executer le requete lors de lappuis du bouton //////////////////////////////////////////

    public void actionPerformed(ActionEvent e){

    Object src=e.getSource();
    if(src instanceof JButton){
    JButton var=(JButton) src;
    ///////////////////////ajouter//////////////////////
    if (var==bt1) {
    try
    {
    st=con.createStatement();
    String req ="INSERT INTO Fournisseur VALUES('"+gett1()+"','"+gett2()+"','"+gett3()+"','"+gett4()+"','"+gett5()+"')";
    int c =st.executeUpdate(req);
    }
    catch(SQLException e1){e1.printStackTrace(); }
    }
    ////////////////////supprimer//////////////
    else if (var==bt2){
    try
    {
    st=con.createStatement();
    String req2="DELETE * FROM Fournisseur WHERE nom =('"+gett2()+"') ";
    int b =st.executeUpdate(req2);
    }
    catch(SQLException e2){e2.printStackTrace(); }

    }
    }}

    }
    //////////////////////////////////////////////////////onglet Client//////////////////////////////////////////////////


    class Client extends JPanel implements ActionListener {
    public Connection con = null;
    public Statement st= null;
    public ResultSet rs= null;



    JLabel l1 = new JLabel(" Id-C");
    JLabel l2 = new JLabel(" Nom-C");
    JLabel l3 = new JLabel(" Prenom-C");
    JLabel l4 = new JLabel(" Adresse");
    JLabel l5 = new JLabel(" N° Telephone");

    JLabel l6 = new JLabel("");
    JLabel l7 = new JLabel("");
    JLabel l8 = new JLabel("");

    JTextField t1 = new JTextField();

    JTextField t2 = new JTextField();
    JTextField t3 = new JTextField();
    JTextField t4 = new JTextField();
    JTextField t5 = new JTextField();


    JButton bt1 = new JButton("Ajouter");
    JButton bt2 = new JButton("Supprimer");

    public JPanel p2 = new JPanel();


    public Client() {




    p2.setLayout(new GridLayout(5, 3, 3,3));
    p2.add(l1);
    p2.add(t1);
    p2.add(bt1);
    p2.add(l2);
    p2.add(t2);
    p2.add(bt2);
    p2.add(l3);
    p2.add(t3);
    p2.add(l6);
    p2.add(l4);
    p2.add(t4);
    p2.add(l7);
    p2.add(l5);
    p2.add(t5);
    p2.add(l8);

    bt1.addActionListener(this);
    bt2.addActionListener(this);

    this.add(p2);
    this.setSize(400,400);

    }

    /////////////// recuperer le text dans le textfield ///////////////////////////////////////////////////////////////////

    public String gett1() {

    return t1.getText();
    }
    //////
    public String gett2() {

    return t2.getText();
    }
    //////
    public String gett3() {

    return t3.getText();
    }
    //////
    public String gett4() {

    return t4.getText();
    }
    //////
    public String gett5() {

    return t5.getText();
    }
    //////////////////////////////executer le requete lors de lappuis du bouton //////////////////////////////////////////

    public void actionPerformed(ActionEvent e){

    Object src=e.getSource();
    if(src instanceof JButton){
    JButton var=(JButton) src;
    ///////////////////////ajouter//////////////////////
    if (var==bt1) {
    try
    {
    st=con.createStatement();
    String req ="INSERT INTO Fournisseur VALUES('"+gett1()+"','"+gett2()+"','"+gett3()+"','"+gett4()+"','"+gett5()+"')";
    int c =st.executeUpdate(req);
    }
    catch(SQLException e1){e1.printStackTrace(); }
    }
    ////////////////////supprimer//////////////
    else if (var==bt2){
    try
    {
    st=con.createStatement();
    String req2="DELETE * FROM Table1 WHERE nom =('"+gett2()+"') ";
    int b =st.executeUpdate(req2);
    }
    catch(SQLException e2){e2.printStackTrace(); }

    }
    }}

    }
    ////////////////////////////////////// onglet Produit ///////////////////////////////////////////////////////////////////////////

    class Produit extends JPanel implements ActionListener {

    public Connection con = null;
    public Statement st= null;
    public ResultSet rs= null;

    //////////////////////////////////////////////////////////


    JLabel l1 = new JLabel(" Nom-P");
    JLabel l2 = new JLabel(" N° Serie");
    JLabel l3 = new JLabel(" Prix");

    JTextField t1 = new JTextField();
    JTextField t2 = new JTextField();
    JTextField t3 = new JTextField();

    JButton bt1 = new JButton("Ajouter");
    JButton bt2 = new JButton("Supprimer");

    public JPanel p3 = new JPanel();

    public Produit() {


    p3.setLayout(new GridLayout(3, 3, 3,3));
    p3.add(l1);
    p3.add(t1);
    p3.add(bt1);
    p3.add(l2);
    p3.add(t2);
    p3.add(bt2);
    p3.add(l3);
    p3.add(t3);

    bt1.addActionListener(this);
    bt2.addActionListener(this);

    this.add(p3);
    this.setSize(400,400);

    }
    /////////////// recuperer le text dans le textfield ///////////////////////////////////////////////////////////////////

    public String gett1() {

    return t1.getText();
    }
    //////
    public String gett2() {

    return t2.getText();
    }
    //////
    public String gett3() {

    return t3.getText();
    }
    //////

    public void actionPerformed(ActionEvent e){

    Object src=e.getSource();
    if(src instanceof JButton){
    JButton var=(JButton) src;
    ///////////////////////ajouter//////////////////////
    if (var==bt1) {
    try
    {
    st=con.createStatement();
    String req ="INSERT INTO Fournisseur VALUES('"+gett1()+"','"+gett2()+"','"+gett3()+"')";
    int c =st.executeUpdate(req);
    }
    catch(SQLException e1){e1.printStackTrace(); }
    }
    ////////////////////supprimer//////////////
    else if (var==bt2){
    try
    {
    st=con.createStatement();
    String req2="DELETE * FROM Table1 WHERE Nom-P =('"+gett1()+"') ";
    int b =st.executeUpdate(req2);
    }
    catch(SQLException e2){e2.printStackTrace(); }

    }
    }}
    }
    //////////////////////////////////////////Application principale ///////////////////////////////////////////////////////////////////

    public class Application extends JFrame implements ActionListener {
    public Connection con = null;
    public Statement st= null;
    public ResultSet rs= null;

    public JMenuBar menu= new JMenuBar();
    JTabbedPane onglet = new JTabbedPane();

    public Application(){


    super("gestion de stock");
    setSize(400,400);
    setVisible(true);
    setDefaultCloseOperation(this.EXIT_ON_CLOSE);

    Container content = this.getContentPane();
    onglet.addTab("Fournisseur",new Fournisseur());
    onglet.addTab("Client",new Client());
    onglet.addTab("Produit",new Produit());
    content.add(BorderLayout.CENTER ,onglet);
    addmenu();
    this.setJMenuBar(menu);
    this.show();

    }

    /////////////////////////////////////////////// menu ////////////////////////////////////////////////////////////////////////////

    public void addmenu() {



    JMenu Fichier = new JMenu("Fichier");
    JMenu Help = new JMenu("Help");
    JMenu Basculer = new JMenu("Basculer");
    JMenuItem Quitter = new JMenuItem("Quiter");
    JMenuItem Fournisseur = new JMenuItem("Fournisseur");
    JMenuItem Client = new JMenuItem("Client");
    JMenuItem Produit = new JMenuItem("Produit");

    menu.add(Fichier);
    menu.add(Help);
    Fichier.add(Quitter);
    Fichier.add(Basculer);

    Basculer.add(Fournisseur);
    Basculer.add(Client);
    Basculer.add(Produit);

    Quitter.addActionListener(this);
    Fournisseur.addActionListener(this);
    Client.addActionListener(this);
    Produit.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
    String cmd = e.getActionCommand();
    if(cmd.equals("Fournisseur"))
    onglet.setSelectedIndex(0);
    if(cmd.equals("Client"))
    onglet.setSelectedIndex(1);
    if(cmd.equals("Produit"))
    onglet.setSelectedIndex(2);
    if(cmd.equals("Quitter"))
    {//JOptionPane.showMessageDialog("ffffffffffffff");
    //int a= Integer.parseInt(JOptionPane.showInputDialog("ok"));
    System.exit(0);
    //this.dispose();

    }
    }


    ////////////////////////////////////se connecter a la base //////////////////////////////////////////////////////////////////
    public void connection(){


    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }
    catch(ClassNotFoundException e5){
    System.out.println("impossible de chargern les drivers");
    }
    try{
    String url="jdbc:odbc:test";
    con=DriverManager.getConnection(url, "","");
    }
    catch(SQLException e5)
    {
    System.out.println("impossible de créer une connexion");
    }
    }


    public void fermeture(){

    try {
    //rs.close();
    st.close();
    con.close();
    }
    catch(SQLException e){}
    }


    public static void main(String[] args) {

    Application interface1 = new Application();

    interface1.connection();
    interface1.fermeture();
    interface1.pack();
    interface1.show();



    }


    }


    merci pour votre aide

  2. #2
    vinou92340
    Invité(e)
    Par défaut
    Bonjour,

    Un petit conseil, tu devrais y aller pas à pas, via la fonction debug d'eclipse et voir où est lancée l'exception exactement (et si c'est situé dans une boucle, à quel moment de l'itération) et les valeurs des paramètres utilisés à ce moment là dont les différentes infos sur l'objet Exception (messages and co qui sont visibles lorsque l'on navigue via l'interface eclipse dans l'objet).
    Il faudrait peut être mettre en gras ces lignes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Application.fermeture(Application.java:436)
    et surtout :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    at Fournisseur.actionPerformed(Application.java:114)
    Enfin, ce n'est qu'un conseil. Voilà chef.

  3. #3
    Membre émérite
    Avatar de olivier.pitton
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2012
    Messages
    355
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2012
    Messages : 355
    Points : 2 814
    Points
    2 814
    Par défaut
    Plop,

    Tu devrais utiliser les balises de code pour l'affichage.

    Je parie sur le fait que tu utilises un peu partout des variables public pour la Connexion, ... et que tu ne les places pas correctement, ainsi à la fermeture ceux-ci ne sont pas initialisés car ils valent null dès le début. Les variables ResultSet et Statement de la classe Application ne servent, à première vue, à rien et donc te font lancer un NullPointerException à la fermeture car tu tentes de fermer une ressource null (le Statement).

    Mon conseil serait d'aller te documenter sur le fameux pattern MVC.

    Bon courage

Discussions similaires

  1. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException ?
    Par freezerhm dans le forum Concurrence et multi-thread
    Réponses: 5
    Dernier message: 04/12/2007, 09h26
  2. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    Par Trint dans le forum Interfaces Graphiques en Java
    Réponses: 6
    Dernier message: 27/02/2007, 11h28
  3. Réponses: 8
    Dernier message: 11/05/2006, 19h32
  4. [JDIC]Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    Par kedare dans le forum Concurrence et multi-thread
    Réponses: 4
    Dernier message: 06/05/2006, 22h45

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo