Salut.

Voilà plus d'une semaine que je tente de modifier un fichier xml avec jdom. J'ai trouvé beaucoup d'aide sur plusieurs sites (y compris celui-ci) et je crois que j'y suis presque. Mais j'ai un message d'erreur, j'espère que vous pourrez m'aider.

Voilà mon fichier xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<connexion>
  <orphy>
    <ip>10.129.50.50</ip>
    <port>21</port>
  </orphy>
  <mysql>
    <host>meteo</host>
    <base>meteo</base>
  </mysql>
</connexion>
Je souhaite modifier dans cet exemple le texte de l'élément "port".

Voici le bout de code que j'ai ecrit (qui s'execute en cliquant sur un bouton):
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
org.jdom.Document document = null;
					Element racine = null;
					String val_port = null;
					SAXBuilder sxb = new SAXBuilder();
					try{
						//On crée un nouveau document JDOM avec en argument le fichier XML
				        //Le parsing est terminé
						document = sxb.build(new File("c:/Program Files/MeteoStat2007/connexion.xml"));
					}
					catch(Exception jdm) {
						jdm.printStackTrace();
					}
					racine = document.getRootElement();
					System.out.println(jTFPort.getText());
					if(! jTFPort.getText().equals("")){
						val_port = jTFPort.getText();
						try{
							racine.getChild("port").setText(val_port);
						}
						catch(Exception exp){
							exp.printStackTrace();
						}
						System.out.println("valeur : "+val_port);
					    try{
					        XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
					        sortie.output(document, new FileOutputStream("c:/Program Files/MeteoStat2007/connexion.xml"));
					    }
					    catch (Exception ex){
					    	System.err.println(ex);
					    }
					}
Et voici le message d'erreur :
java.lang.NullPointerException
at stat_meteo.meteorologie$1.actionPerformed(meteorologie.java:148)
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.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)
La ligne 148 (qui pose probleme) : racine.getChild("port").setText(val_port);

J'ai déjà posté sur un autre forum, mais sans succès, celui qui a essayé de m'aider n'a pas compris d'où ça venait. S'il vous plait, c'est pour mon projet de fin d'année, et je bloque là dessus! C'est important.

Merci d'avance.