Bonsoir tous le monde,
est ce qu'il y a une personne qui peut me dire pourquoi ce programme ne marche pas ? il est très simple pourtant.
Code :
- import java.io.*;
- import org.jdom.*;
- import org.jdom.input.*;
- import org.jdom.filter.*;
- import java.util.List;
- import java.util.Iterator;
- public class Essaye2
- {
- static org.jdom.Document document;
- static Element racine;
- public static void afficheALL()
- {
- //On crée une List contenant tous les noeuds "etudiant" de l'Element racine
- List listEtudiants = racine.getChildren("etudiant" );
- //On crée un Iterator sur notre liste
- Iterator i = listEtudiants.iterator();
- Element courant = (Element)i.next();
- //On affiche le nom de l'element courant
- System.out.println(courant.getChild("nom" ).getText());
- System.out.println(courant.getChild("prenom" ).getText());
- }
-
- public static void main(String[] args)
- {
- //On crée une instance de SAXBuilder
- 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(MonFichier())); //normalement c MonFichier.xml
- }
- catch(Exception e){}
- //On initialise un nouvel élément racine avec l'élément racine du document.
- racine = document.getRootElement();
- afficheALL();
- }
-
- public static String MonFichier()
- {
- return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
- "<personnes>"+
- "<etudiant >"+
- "<nom>MonNom</nom>"+
- "<prenom>MonPrenom</prenom>"+
- "</etudiant>"+
- "</personnes>";
- }
-
- }
|
aucune erreur mais quand je lance l'exécution une exéption s'affiche :
Code :
- Exception in thread "main" java.lang.NullPointerException
- at Essaye2.main(Essaye2.java:39)
|