Bonjour!
je suis en train de me casser la tete sur un problème que je ne comprend pas du tout.
En fait j'instancie une classe BaseDeDonneesImpl lorsque que je clic sur le bouton "connecter" de mon JFrame.
Code cd cmdbOk
La variable implBaseDeDonnees est une variable de l'instance FrmAccueil.
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
45
46
47 private JButton getCmdbOk() { if (cmdbOk == null) { cmdbOk = new JButton(); cmdbOk.setText("Ok"); cmdbOk.setMnemonic(KeyEvent.VK_O); cmdbOk.setDefaultCapable(true);//pourquoi il n'est pas par défaut????? cmdbOk.addActionListener(new CmdbConnecterListener()); return cmdbOk; } return cmdbOk; } //SOUS CLASSE--------------------------------------------------------------------- class CmdbConnecterListener implements ActionListener { private JOptionPane alertBox=null; public void actionPerformed(java.awt.event.ActionEvent e) { String host="192.168.0.22"; //INSTANCIATION implBaseDeDonnees = new BaseDeDonneesImpl("config.XML",host, "1521", "orcl", getTxtUtilisateur().getText(),new String(getTxtPassWord().getPassword())); //setImplBaseDeDonnees("config.XML",host, "1521", "orcl", getTxtUtilisateur().getText(),new String(getTxtPassWord().getPassword())); try { gradientPainter.remove(jPanCenter); implBaseDeDonnees.getConnection(); chargeListMarque(); PanSelParcArbreParc panSelParcArbreParc=new PanSelParcArbreParc(getThis(),implBaseDeDonnees); implBaseDeDonnees.deconnecter(); panSelParcArbreParc.setOpaque(false); gradientPainter.add(panSelParcArbreParc,BorderLayout.CENTER); gradientPainter.revalidate(); gradientPainter.repaint(); maximize(); } catch (LectureBDDException e1) { if (e1.getCodeErreur()==LectureBDDException.PASSWORDERROR) alertBox = new JOptionPane(); JOptionPane.showMessageDialog(getThis(), "mot de passe incorrect", "Alerte",JOptionPane.WARNING_MESSAGE); alertBox.setVisible(true);//show(); } catch (SQLException SQLe) { SQLe.printStackTrace(); alertBox = new JOptionPane(); JOptionPane.showMessageDialog(getThis(), "Connection non fermée", "Alerte",JOptionPane.WARNING_MESSAGE); alertBox.setVisible(true);//show(); } } }
Durant cet actionPerformed je lis aussi TOUTE la base sans problème.
Les panels suivant m'affiche bien ce que je viens de lire.
C'est maintenant que tout se corse.
Lorsque je souhaite me reconnecter à la base
actionPerformed de btnLireDisqueDur
frmPere fait référence à la JFrame FrmAccueil de tout à l'heure. En fait lorsque j'instancie mes panels je mets en premier paramètre le FrmAccueil
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 BaseDeDonneesImpl b=((FrmAccueil)frmPere).getImplBaseDeDonnees(); Connection con=b.getConnection(); b.lireDisqueDur(1);
Tout me semble correct, mais je recois cette exception
Voici la méthode lireDisqueDur. la ligne 837 fait référence à rst.close mais c'est normal que j'obtienne un NullPointer vu que l'erreur vient réellement de rst = lireBDD(rqtSQL, wildCard);
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 2 mai 2009 03:34:37 baseDeDonnees.BaseDeDonneesImpl lireBDD ATTENTION: Connexion interrompue Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at baseDeDonnees.BaseDeDonneesImpl.lireDisqueDur(BaseDeDonneesImpl.java:837) at graphique.panel.PanMachine$InscritListener.actionPerformed(PanMachine.java:1027) 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.AWTEventMulticaster.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.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) 2 mai 2009 03:35:26 baseDeDonnees.ConnectionToBDD closeConnection INFO: Déconnection de la base
vu que tout cela vient de lireBDD(rqtSQL, wildCard) je vous met donc la méthode
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 public DisqueDur lireDisqueDur(int idDisqueDur) throws LectureBDDException { log.log(Level.INFO, "Début de lecture du Disque Dur id:" + idDisqueDur); String[] wildCard = { String.valueOf(idDisqueDur) }; String rqtSQL = "Select * From DISQUEDUR Where idDisqueDur = ?"; ResultSet rst = null; DisqueDur disqueDur=null; try { rst = lireBDD(rqtSQL, wildCard); disqueDur = initDisqueDur(rst, 1); log.log(Level.INFO, "Disque Dur id:" + idDisqueDur + " lu"); } catch (LectureBDDException e) { throw new LectureBDDException(e.getMessage()); }finally{ try { rst.close(); } catch (SQLException e) { String message="Le resultSet n'a pas été clos."; log.log(Level.WARNING,message); e.printStackTrace(); throw new LectureBDDException(message); } } return disqueDur; }
le problème se produit au niveau du PreparedStatement, j'en déduit donc que le souci provient de la classe ConnectionToBDD que voici:
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 private ResultSet lireBDD(String rqtSQL, String[] wildCard) throws LectureBDDException { ResultSet rst = null; try { PreparedStatement pst = this.getConnection().prepareStatement( rqtSQL, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); for (int i = 0; i < wildCard.length; i++) pst.setObject(i + 1, wildCard[i]); rst = pst.executeQuery(); } catch (SQLException e) { // TODO Auto-generated catch block log.log(Level.WARNING, e.getMessage()); } return rst; }
Donc je ne comprend pas pourquoi lors de la première instanciation, dans l'actionPerformed tout se passe bien. Mais quand je passe par le getter FrmAccueil.getImplBaseDeDonnees(), rien ne marche.
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
45 public class ConnectionToBDD { // CachedRowSet crs=new CachedRowSetImpl(); private static Logger log = Logger.getLogger(ConnectionToBDD.class .getName()); private static Connection connection; public static Connection getConnection(BaseDeDonneesImpl BDDImpl) throws LectureBDDException { if (connection == null) { try { DriverManager.registerDriver((Driver) Class.forName(BDDImpl.getNomDriver()).newInstance()); connection = DriverManager.getConnection(BDDImpl.getUrl(),BDDImpl.getUser().getNomUser(), BDDImpl.getUser().getPassword()); } catch (SQLException e) { String info = ""; if (e.getErrorCode() == 28000) info = "Mot de passe erroné."; System.out.println(e.getErrorCode()); log.log(Level.WARNING, "La connection a échouée. " + info); throw new LectureBDDException(info, LectureBDDException.PASSWORDERROR); } catch (ClassNotFoundException e) { log.log(Level.WARNING, "La class " + BDDImpl.getNomDriver() + " est introuvable. " + e.getMessage()); System.exit(0); } catch (InstantiationException e) { log.log(Level.WARNING, e.getMessage()); } catch (IllegalAccessException e) { log.log(Level.WARNING, e.getMessage()); } log.log(Level.INFO, "Connection à la base " + BDDImpl.toString()+ " réussie."); } return connection; } public static void closeConnection() throws SQLException { try { if (connection != null) connection.close(); log.log(Level.INFO, "Déconnection de la base"); } catch (SQLException e) { log.log(Level.WARNING, "Déconnection impossible."); throw new SQLException ("Déconnection impossible"); } } }
pourtant si je fais un println(bddImpl.toString()) dans le bouton btnOk et println(((FrmAccueil)frmPere).getImplBaseDeDonnees().toString()) dans l'actionPerformed de btnLireDisqueDur j'obtiens bien les mêmes résultats
quelqu'un verrait-il une piste à mon problème????
bon je ne sais pas si j'ai été assez clair, n'hésitez pas à me demander des précisions, j'y répondrait du mieux que je pourrais.
merci d'avance.
Partager