Bonjour à tous, je suis étudiant en 3ème année d'informatique à Amiens et j'ai a réaliser un projet qui consiste en la création d'un pendu. J'ai comme consigne d'utiliser un arbre lexicographique comme base de donnée afin de stocker mes mots.

Je vous présente donc mon code :

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package Model;
 
import java.io.*;
 
 
 
public class ArbreLexicographique {
	private  Noeud courant;
	private  Noeud tete;
	private char lettre;
 
 
 
	public ArbreLexicographique() {
 
		Noeud[] fils= new Noeud[30];
		this.tete= new Noeud('\u0000', false, 1, fils);
		this.courant=tete;
		this.RemplirArbre();
 
 
	}
 
	private void ajouterMot(String mot){
		int i, nbFils;
		System.out.println(mot);
		if (mot.equals("")){
 
			courant.setMot(true);	
			this.courant=tete;
 
 
		}
		else
			lettre=mot.charAt(0);
		if (possedeFils(this.courant.getFils(), this.lettre)){
			i=chercherFils(this.courant.getFils(), this.lettre);
			this.courant = this.courant.getFils()[i];
			System.out.println(mot);
			if (mot.equals("")){
 
				courant.setMot(true);	
				this.courant=tete;
			}
			else {
				mot=mot.substring(1,mot.length());
				ajouterMot(mot);	
			}
		}
		else {
 
			nbFils = nbFils(this.courant.getFils());
			Noeud[] fils;
			this.courant.getFils()[nbFils]= new Noeud(lettre, false, this.courant.getNiveau()+1, fils= new Noeud[30]);
			i=chercherFils(this.courant.getFils(), lettre);
			this.courant = this.courant.getFils()[i];
			if (mot.equals("")){
 
				courant.setMot(true);	
				this.courant=tete;
			}
			else {
				mot=mot.substring(1,mot.length());
				ajouterMot(mot);
			}
		}
 
	}
 
	String GenererMotAleatoire() {
		int random, random2, i;
		this.courant=tete;
		char [] mot = null;
		String mot2 = null;
		random=nbAleatoire(1, 6);
		for (i=0; i<random; i++){
			random2=nbAleatoire(0, nbFils(courant.getFils()));
			System.out.println(random2);
			char[] cs = mot;
			cs[i]= courant.getFils()[random2].getLettre();
			if (courant.getFils()[i].isMot()){
				mot2= new String(cs);
			}
			courant= courant.getFils()[random2];
		}
		return mot2;
	}
 
	private int nbAleatoire(int lower, int higher){
		int random = (int)(Math.random() * (higher-lower)) + lower;
		return random;
	}
 
 
 
	private boolean possedeFils(Noeud[] fils, char c){
		int i;
		Boolean bool=false;
		for (i=0; i<fils.length; i++){
			if(fils[i]== null){
				continue;
			}
			else if(fils[i].getLettre() == c){
				bool=true;
			}
		}
		return bool;
	}
 
	private int chercherFils(Noeud[] fils, char c){
		int i, j;
		j=0;
 
		for (i=0; i<fils.length; i++){
			if(fils[i]== null){
				continue;
			}
			else if(fils[i].getLettre() == c){
				j=i;
			}
		}
		return j;
	}
 
	private int nbFils (Noeud[] fils){
		int i, cpt;
		cpt=0;
		for (i=0; i<fils.length; i++){
			if (fils[i] != null ){
				cpt++;
			}
		}
		return cpt;
	}
 
	private void RemplirArbre(){
		String fichier ="C:/Documents and Settings/Propriétaire.VIN-1D00C6226C7/Bureau/eclipse-java-indigo-SR1-win32/Pendu/src/Model/dico.txt";
 
 
		try{
 
			InputStream ips=new FileInputStream(fichier); 
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br=new BufferedReader(ipsr);
			String ligne;
			int i=0;
			while ((ligne=br.readLine())!=null){
				this.ajouterMot(ligne);
				i++;
				System.out.println(i);
 
			}
			br.close(); 
		}		
		catch (Exception e){
			e.printStackTrace();
		}
	}		
 
 
	public Noeud getCourant() {
		return courant;
	}
 
	public void setCourant(Noeud courant) {
		this.courant = courant;
	}
 
	public Noeud getTete() {
		return tete;
	}
 
	public void setTete(Noeud tete) {
		this.tete = tete;
	}
 
	public char getLettre() {
		return lettre;
	}
 
	public void setLettre(char lettre) {
		this.lettre = lettre;
	}
}
J'ai un fichier texte contenant tout les mots de la langue francaise. A partir de celui-ci, j'utilise une fonction qui permet de lire le fichier ligne par ligne (voir la fonction remplirArbre()) et d'ajouter le mot correspondant à l'arbre (voir la fonction ajouterMot()). Cependant j'ai un soucis, Dans la fonction qui doit me permettre de générer un mot aléatoirement et qui est stocké dans l'arbre, j'ai une java.lang.NullPointerException. Elle se situe dans la fonction GénererMotAléatoire() à cette ligne :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
cs[i]= courant.getFils()[random2].getLettre();
Le noeud en question est vide, c'est pour cela que je ne peux pas appliquer getLettre. Pourtant grâce a Random2 j'ai bien le nombre de fils du noeud père et donc je ne peux pas déborder sur une case suivante du tableau qui serait à null.

Je pense donc que le problème vient de l'ajout des mots qui ne se fait pas comme je le souhaite. J'ai essayer de faire des modifications mais sans succès, j'aimerais bien un petit coup de main.