Bonjour,
Je n'ai pas compris ce problème " Null pointer exeption".
J'ai instancier l'objet ob de type TypeInterpretaion avant de l' ajouter interpretation de type ArrayList<TypeInterpretation>
comme ceci
la classe TypeInterpretation
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 TypeInterpretation ob= new TypeInterpretation(name, nbrOfAtt, attri); this.interpretation.add(ob);
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 public class TypeInterpretation { private String nameOfType ; private int nbrOfAttributes; private Map<String,String> attributes = new HashMap<>(); public TypeInterpretation() { super(); // TODO Auto-generated constructor stub } public TypeInterpretation(String nameOfType, int nbrOfAttributes, Map<String, String> attributes) { super(); this.nameOfType = nameOfType; this.nbrOfAttributes = nbrOfAttributes; this.attributes = attributes; } //avec les setters et les getters
Le but est d'implémenter une méthode du classe ObjectResult qui rend en paramètre le nom de fichier xml à parser et l'id et qui va récuperer les données du fichier selon une condition que l'id de l'élément attribute doit égale à l'id passé en paramètre.
la classe ObjectResult
La récupération se fait avec l'ajout de ces éléments dans interpretation de type ArrayList<TypeInterpretation>.
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 private Object result; private ArrayList<TypeInterpretation> interpretation = new ArrayList<TypeInterpretation>() ; //les constructeurs et les getters et les setters public ArrayList<TypeInterpretation> getInterpretation(String path,String id) { //Creation of our factory XMLInputFactory factory = XMLInputFactory.newInstance(); //give path File file = new File(path); try { //get reader XMLStreamReader reader = factory.createXMLStreamReader(new FileReader(file)); while (reader.hasNext()) { // get the event int type = reader.next(); if(type== XMLStreamReader.START_ELEMENT && reader.getLocalName()=="attribute" && reader.getAttributeName(0).toString() == "id" && reader.getAttributeValue(0).equals(id)) { Boolean found=false ; while (reader.hasNext()&& !found) { type = reader.next(); switch (type) { case XMLStreamReader.START_ELEMENT: { String name = reader.getLocalName(); int nbrOfAtt = reader.getAttributeCount(); Map<String,String> attri = new HashMap<>(nbrOfAtt); for(int i = 0; i < reader.getAttributeCount(); i++) { attri.put(reader.getAttributeName(i).toString(), reader.getAttributeValue(i)) ; } TypeInterpretation ob= new TypeInterpretation(name, nbrOfAtt, attri); this.interpretation.add(ob); break; } case XMLStreamReader.END_ELEMENT: if(reader.getLocalName()=="attribute") { found=true; } break; } } } else //attribute is not found interpretation=null; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (XMLStreamException e) { e.printStackTrace(); } return interpretation ; }
'Lerreur est Null pointer exeption due au ligneMerci d'avance
Code : Sélectionner tout - Visualiser dans une fenêtre à part this.interpretation.add(ob);
Partager