NULLPOINTEREXCEPTION dans la classe Groupe. impossibilité de trouver un tableau d'objets
Bonjour à tous,
je rencontre un probleme dans mon programme. Lorsque je veux tester la classe GROUPE, la méthode toString me retourne un objet null et je n'arrive pas à cerner ou se trouve le problème.
merci d'avance pour vos réponses.
Classe groupe
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 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
| public class Groupe {
//attributs
private Etudiant tabEtud[];
private int nbEtudiant;
public Groupe() {
tabEtud= new Etudiant[24];
}
public void ajouter( Etudiant etud){
tabEtud[nbEtudiant]=etud;
nbEtudiant++;
}
public String toString(){
String mes="";
for(int i=0;i<tabEtud.length;i++){
mes+=tabEtud[i]+" ";
}
return mes;
}
public Etudiant getTabEtud(int i) {
return tabEtud[i];
}
public Etudiant[] getTabEtud(){
return tabEtud;
}
public void setTabEtud(Etudiant[] tabEtud) {
this.tabEtud = tabEtud;
}
public int getNbEtudiant() {
return nbEtudiant;
}
public void setNbEtudiant(int etudiant) {
this.nbEtudiant = etudiant;
}
public double meilleure(){
double max=0.;//moyenne plus éléve
for(int i=0;i<nbEtudiant;i++){
double val=this.tabEtud[i].getEvaluation().calculerMoyenne();
/*if(val>max){
max=val;
}*/
max= Math.max(max, val);
}
return max;
}
public double pire(){
double min=100.;//moyenne la plus basse
for(int i=0;i<nbEtudiant;i++){
double val=this.tabEtud[i].getEvaluation().calculerMoyenne();
/*if(val<min){
min=val;
}*/
min= Math.min(val, min);
}
return min;
}
public int rechercher(String code){
Etudiant etud = new Etudiant(code,null,null);
return TabUtils.rechSequentielle(tabEtud, etud, nbEtudiant);
}
public void trier(){
TabUtils.tri(tabEtud, nbEtudiant);
}
} |
classe Etudiant
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| public class Etudiant {
private String code;
private String nom;
private Notes evaluation;
//constructeur avec paramètre
/**
*
* @param code
* @param nom
* @param eval
*/
public Etudiant(String code, String nom, String eval) {
/*setCode(code);*/this.code = code;
/*setNom(nom);*/this.nom = nom;
this.evaluation=new Notes(eval);
}
//constructeur par défaut
public Etudiant(){
this("0","inconnu","0");
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public Notes getEvaluation() {
return evaluation;
}
public void setEvaluation(Notes evaluation) {
this.evaluation = evaluation;
}
/**
* methode "message" qui affiche
* Echec si la moyenne de l'étudiant est en-dessous de 60
* et réussite si c'est le contraire
* @return
*/
public Message message(){
if(this.evaluation.calculerMoyenne()<60)
return Message.ECHEC;
else
return Message.REUSSITE;
}
public boolean equals(Etudiant autre){
boolean res=this.code.equals(autre.code);
return res;
}
public int compareTo(Etudiant autre){
int qqch= this.code.compareTo(autre.code);
return qqch;
}
} |
Classe Notes
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
public class Notes {
//attribut
private double tabNotes[];
private int nbNotes=0;
//constructeur
/**
*
* @param tableau de notes
*/
public Notes(double[] tabNotes) {
this.tabNotes = tabNotes;
nbNotes=tabNotes.length;
}
//autre constructeur
public Notes(String sNotes){
//remplir tabNotes
remplirTableau(sNotes);
nbNotes=tabNotes.length;
}/**méthode d'affichage des notes
*parcours le tableau de valeur créer
*en paramètre dans les constructeurs
**/
public String toString(){
String res="";
for(double note: tabNotes)
res+=note+" ";
return res;
}
/**méthode pour calculer la moyenne*/
public double calculerMoyenne(){
double somme=0;
double moy=0;
for(double note: tabNotes){
somme+=note;
}
if(nbNotes!=0)
moy= somme/nbNotes;
return moy;
}
public int getNbNotes() {
return nbNotes;
}
public void setNbNotes(int nbNotes) {
this.nbNotes = nbNotes;
}
public double getTabNotes(int i) {
return tabNotes[i];
}
public void setTabNotes(double notes,int i) {
this.tabNotes[i] = notes;
}
public void remplirTableau(String sNotes){
//System.out.println("Contenus snotes : " + sNotes);
String separe[]=sNotes.split("[, ]");
tabNotes= new double[separe.length];
for(int i=0;i<separe.length;i++){
tabNotes[i]=Double.parseDouble(separe[i]);
}
}
} |
test de la classe groupe
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
Groupe gr = new Groupe();
Etudiant etud2 = new Etudiant("26161234", "Marc", "65 81 58 100 79");
gr.ajouter(etud2);
Etudiant etud=new Etudiant("24910003", "Pierre", "45 59 36 66");
gr.ajouter(etud);
//afficher le groupe d'étudiants
System.out.println("Groupe d'étudiants:\n" + gr.toString()); |