Bonjour a tous, voici tout dabord mon code, que j ai reduit au maximum
Je transforme en fait un fichier texte en document XML.Code:
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
48
49
50
51 import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.xpath.XPath; public class Main { static Element root2 = new Element("itf"); public static void main(String[] args) { String itf = "path_de_mon_fichier"; String s2 = File_processing.itfReader(itf,root2); root2 = Xml_transform.addHeader(s2,root2); root2 = Xml_transform.addPrimary(s2,root2); root2 = Xml_transform.addLayers(s2,root2); try { addHeightProperty(root2); } catch (JDOMException e) { e.printStackTrace(); } } public static void addHeightProperty (Element root) throws JDOMException { List<Element> listLayers = root.getChildren("layer"); int nb_Layer_XML2 = integer.parseInt((XPath.newInstance("count(//layer").valueOf(root))); System.out.println(nb_Layer_XML2); for (int i=1;i<listLayers.size();i++) { System.out.println((listLayers.get(i)).getAttributeValue("name")); XPath xpa = XPath.newInstance("//layer[@name=\""+(listLayers.get(i)).getAttributeValue("name")+"\"]"); Element a = (Element) xpa.selectSingleNode(root); System.out.println(a.getAttributeValue("name")); } } } } }
J applique plusieurs autres methodes qui fonctionnent correctement et j appelle ensuite une methode addHeightProperty.
Je creer une liste des element "layer" pour les rechercher ensuite dans le xml
Mon element root n est pas vide
Ma liste n est pas vide non plus
Il contiennent tous les deux des elements "layer"
Cependant, lorsque j essaie de compter mes layers avec du xpath, java m affiche 0
Avec un :je peux printer ce que je veuxCode:System.out.println((listLayers.get(i)).getAttributeValue("name"));
Mais
lorsque je fais ca
j ai cette erreurCode:
1
2
3
4
5 XPath xpa = XPath.newInstance("//layer[@name=\""+(listLayers.get(i)).getAttributeValue("name")+"\"]"); System.out.println(xpa.toString()); Element a = (Element) xpa.selectSingleNode(root); System.out.println(a.getAttributeValue("name")); //ici la ligne 56 pour l erreur
A savoir egalement, que le meme code exactement,mais dans un workspace et une machine different, fonctionne parfaitement.Code:
1
2
3
4 Exception in thread "main" java.lang.NullPointerException at Main.addHeightProperty(Main.java:56) at Main.main(Main.java:28)
en trippotant un peu la boucle et tout ca j arrive avec une IndexOutofBound index 168, alors que la taille max de ma liste est de 73...
J ai essaye de m exprimer au plus clair
Quelqu un peut il m aider?