Exception in thread "main"

Résolu
puisssam -  
puisssam Messages postés 1 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour à tous , après avoir chercher des heures je ne comprends pas du tout pourquoi ma compilation plante alors que mon code ne signale aucune erreur : c'est un programme visant à créer des objets "étudiant" à partir d'un fichier texte voici mon message d'erreur :

Exception in thread "main" java.lang.NullPointerException
at java.util.StringTokenizer.<init>(StringTokenizer.java:182)
at java.util.StringTokenizer.<init>(StringTokenizer.java:204)
at LesEtudiants.fabriqueEtudiant(LesEtudiants.java:37)
at LesEtudiants.remplirVecteur(LesEtudiants.java:55)
at LesEtudiants.main(LesEtudiants.java:75)


ET VOICI MON CODE CONTENANT 2 CLASSE


public class Etudiant {


private String intitule;
private int numero;
private String nom;
private String prenom;

public Etudiant(String intitule,int numero, String nom, String prenom){

this.intitule=intitule;
this.numero=numero;
this.nom=nom;
this.prenom=prenom;

}

public Etudiant(){
this("",0,"","");
}


public void getintitule(){
System.out.println(intitule);
}

public void setintitule(String i){
this.intitule=i;
}

public void getnumero(){
System.out.println(numero);
}

public void setnumero(int nu){
this.numero=nu;
}

public void getnom(){
System.out.println(nom);
}

public void setnom(String no){
this.nom=no;
}

public void getprenom(){
System.out.println(prenom);
}

public void setprenom(String p){
this.prenom=p;
}

public void afficher(){
System.out.println("matière : "+intitule+"numero : "+numero+"nom : "+nom+"prenom : "+prenom);
}


}



import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;


public class LesEtudiants {

private Vector<Etudiant> etudiants;
private BufferedReader bfr;

public LesEtudiants(){
etudiants=null;
bfr=null;
}

public LesEtudiants(String Fichier){
etudiants= new Vector<Etudiant>();

try{

FileReader in = new FileReader(Fichier);
bfr=new BufferedReader(in);

}catch (IOException e){
System.out.println("Pb lecture du fichier:"+ e.getMessage());
}


}

private Etudiant fabriqueEtudiant(String phrase){
Etudiant ent=null;

StringTokenizer st = new StringTokenizer(phrase,";");


if(st.countTokens()==4){
ent=new Etudiant();
ent.setintitule(st.nextToken());
ent.setnumero(Integer.parseInt(st.nextToken()));
ent.setnom(st.nextToken());
ent.setprenom(st.nextToken());
}
return ent;
}

public void remplirVecteur() throws IOException {
String phrase;
Etudiant ent;
do{
phrase=bfr.readLine();
ent=fabriqueEtudiant(phrase);
etudiants.add(ent);
}while (phrase!=null);
}

public Vector<Etudiant> getEtudiants(){
return etudiants;
}

public void afficher(){
Iterator<Etudiant> it=etudiants.iterator();
while(it.hasNext()){
it.next().afficher();
}
}

public static void main(String [] args){

LesEtudiants LesEnts = new LesEtudiants("CSV.txt");
try{
LesEnts.remplirVecteur();
}catch (IOException e){
System.out.println("Pb de lecture : "+e.getMessage());
}
LesEnts.afficher();
}

}



et mon fichier TEXTE CSV

biologie;20593829;durant;alber
math;29482940;dupuis;tom
français;30923984;chevalier;artur
histoire;39048208;dupont;maxime



EN espérant que vous pourrez m'aider merci d'avance.

1 réponse

KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
"pourquoi ma compilation plante alors que mon code ne signale aucune erreur"
1) ce n'est pas le code qui signale les erreurs, mais le compilateur
2) s'il n'y a pas d'erreur ce n'est donc pas la compilation qui plante, mais l'exécution

"mon fichier TEXTE CSV"
pour info, dans ton code il s'appelle CSV.txt

En ce qui concerne l'erreur en elle même, elle vient de remplirVecteur.
Tu as bien pensé à tester que phrase pouvait devenir null, mais tu as placé le test au mauvais endroit puisque tu envoies quand même null dans fabriqueEtudiant !
Voici une correction possible de cette méthode, le reste de l'exécution ne pose pas de problème, même si le résultat ne donne peut-être pas tout à fait ce que tu attendais...

public void remplirVecteur() throws IOException
{
	String phrase;
	while ((phrase=bfr.readLine()) != null)
		etudiants.add(fabriqueEtudiant(phrase));
}
2
arth Messages postés 9374 Date d'inscription   Statut Contributeur Dernière intervention   1 293
 
Magnifique explication ! Oh notre DIeu Java à tous ! ;-)

Pas mieux.
0
puisssam Messages postés 1 Date d'inscription   Statut Membre Dernière intervention  
 
Merci ça marche , je te remercie mais vraiment pour cette superbe explication merci merci :
0