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 :

probleme avec Exception java.lang.NullPointerException


Sujet :

JDBC Java

  1. #1
    Candidat au Club
    Inscrit en
    Décembre 2006
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 6
    Points : 2
    Points
    2
    Par défaut probleme avec Exception java.lang.NullPointerException
    salut tout le monde
    voici ma méthode ou le problème est signalé

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    public void Affichetableau(JTable tbl) throws SQLException {
    	String sql = "select * from auto";
    
    		try {
    			System.out.println("aaaaaaaaaaaaaa");
    			prepard= cnx.prepareStatement(sql);
    			System.out.println("bbbbbbbbbbbbbb");
    		    rs= prepard.executeQuery(sql);
    		    System.out.println("cccccccccccccc");
    			tbl.setModel(DbUtils.resultSetToTableModel( rs));
    			System.out.println("ddddddddddddddd");
    		}catch (SQLException e) {
    			
    			JOptionPane.showMessageDialog(null, e);
    		
    	}
    	
    }
    et voici l'exception qui apparaitre
    Exception in thread "AWT-EventQueue-0 java.lang.NullPointerException
    at Login.Affichetableau(Login.java:4617)
    la ligne 4617 c'est la ligne en couleur
    merci d'avance

  2. #2
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

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

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Points : 29 131
    Points
    29 131
    Billets dans le blog
    2
    Par défaut
    Salut,

    Tu est sûr que ce n'est pas plutôt prepard= cnx.prepareStatement(sql);, l'instruction qui provoque l'erreur, ou du moins, une autre ligne que celle que tu dis (parce que ni sql ni prepard n'ont de raisons d'être null) ? Le mieux est de nous montrer la stacktrace complète.

    Par ailleurs, il y a un vrai souci là : Exception in thread "AWT-EventQueue-0 java.lang.NullPointerException. Cela veut dire que tu fais une requête SQL dans l'Event Dispatcher, ce qui risque de provoquer des gels de ton UI. Il vaut mieux utiliser un SwingWorker pour faire ce genre de chose.

  3. #3
    Candidat au Club
    Inscrit en
    Décembre 2006
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 6
    Points : 2
    Points
    2
    Par défaut
    merci pour votre réponse
    c'est tt se que apparaître au console
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    aaaaaaaaaaaaaa
    bbbbbbbbbbbbbb
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Login.Affichetableau(Login.java:4617)
    	at Login$4.actionPerformed(Login.java:4551)
    	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.AbstractButton.doClick(Unknown Source)
    	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)
    exit
    j'ais mis les instructions system.out.println pour avoir la dernière instruction exécutée avant l'erreur
    et tant-que l'instruction System.out.println("bbbbbbbbbbbbbb"); est exécutée donc normalement il n'y a pas de problème on création de Statement
    mercu une autre fois

  4. #4
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

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

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Points : 29 131
    Points
    29 131
    Billets dans le blog
    2
    Par défaut
    Tu peux essayer avec :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    System.out.println("Avant prepareStatement: cnx="+cnx);
    			prepard= cnx.prepareStatement(sql);
    			System.out.println("avant executeQuery: prepard=" + prepard);
    		    rs= prepard.executeQuery(sql);
    C'est quoi comme DB ?

  5. #5
    Candidat au Club
    Inscrit en
    Décembre 2006
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 6
    Points : 2
    Points
    2
    Par défaut
    Citation Envoyé par joel.drigo Voir le message
    Tu peux essayer avec :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    System.out.println("Avant prepareStatement: cnx="+cnx);
    			prepard= cnx.prepareStatement(sql);
    			System.out.println("avant executeQuery: prepard=" + prepard);
    		    rs= prepard.executeQuery(sql);
    C'est quoi comme DB ?
    merci mon ami
    la BD mysql se trouve sur phpmyadmin

  6. #6
    Candidat au Club
    Inscrit en
    Décembre 2006
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 6
    Points : 2
    Points
    2
    Par défaut
    Citation Envoyé par joel.drigo Voir le message
    Tu peux essayer avec :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    System.out.println("Avant prepareStatement: cnx="+cnx);
    			prepard= cnx.prepareStatement(sql);
    			System.out.println("avant executeQuery: prepard=" + prepard);
    		    rs= prepard.executeQuery(sql);
    C'est quoi comme DB ?
    et c la nouvelle Exception
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    Avant prepareStatement: cnx=Login$1@4fc4b003
    Exception in thread "AWT-EventQueue-0" avant executeQuery: prepard=null
    java.lang.NullPointerException
    	at Login.Affichetableau(Login.java:4621)
    	at Login$4.actionPerformed(Login.java:4551)
    	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.AbstractButton.doClick(Unknown Source)
    	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)
    exit

  7. #7
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

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

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Points : 29 131
    Points
    29 131
    Billets dans le blog
    2
    Par défaut
    Ok, donc prepard est null. Cela me semble bizarre qu'une implémentation de Connection puisse retourner un statement null, surtout un pilote de MySql. Tu peux montrer le code qui crée l'instance de Connection ?

  8. #8
    Candidat au Club
    Inscrit en
    Décembre 2006
    Messages
    6
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 6
    Points : 2
    Points
    2
    Par défaut
    Citation Envoyé par joel.drigo Voir le message
    Ok, donc prepard est null. Cela me semble bizarre qu'une implémentation de Connection puisse retourner un statement null, surtout un pilote de MySql. Tu peux montrer le code qui crée l'instance de Connection ?
    merci une autre fois pour votre aide
    voice le code de connexion au BD

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public static java.sql.Connection connect() {
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    			java.sql.Connection con= DriverManager.getConnection("jdbc:mysql://localhost/autorisation", "root","");
    			JOptionPane.showMessageDialog(null, "Connection DB avec succès");
    			return con;
    		}catch(Exception e) {
    			JOptionPane.showMessageDialog(null, e);
    			return null;
    		}
     
     
    	}
    donc je doit changer l’instance cnx par con et le problème est résolue
    merci mon ami infiniment

  9. #9
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Points : 48 807
    Points
    48 807
    Par défaut
    cnx est de type Login$1. Faudrait que tu nous montre cette classe parce que c'est manifestement elle qui te retourne null sur prepareStatement.

Discussions similaires

  1. Combobox avec l'exception "java.lang.NullPointerException"
    Par FatmaFafa dans le forum Débuter avec Java
    Réponses: 2
    Dernier message: 10/08/2012, 16h46
  2. une exception java.lang.NullPointerException
    Par marwa21 dans le forum Struts 1
    Réponses: 8
    Dernier message: 22/07/2010, 12h49
  3. [Exception] java .lang.NullPointerException
    Par Deallyra dans le forum Général Java
    Réponses: 8
    Dernier message: 22/05/2008, 16h25
  4. Exception : java.lang.NullPointerException
    Par parano dans le forum Langage
    Réponses: 4
    Dernier message: 05/07/2007, 20h27
  5. Erreur exception java.lang.NullPointerException
    Par geol99 dans le forum Langage
    Réponses: 2
    Dernier message: 14/06/2007, 20h24

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