Problème pour jeu de la vie
Bonsoir,
Après moulte recherches je ne trouve pas la solution à mon problème. Je suis sure que c'est une erreur de bleu mais bon voila mon code:
Game.java
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
|
package vie;
import java.util.HashSet;
class Game {
private HashSet<Coord> damier;
private int nbLigs, nbCols;
//constructeur
public Game(Coord[] listeInitiale, int nbLigs, int nbCols){
this.nbLigs = nbLigs;
this.nbCols = nbCols;
for(int i = 0;i<listeInitiale.length;i++){
this.damier.add(listeInitiale[i]);
}
}
public Game(int nbLigs, int nbCols){
this.damier.clear();
}
public boolean estVivante(Coord coord){
return damier.contains(coord);
}
private String showCell(Coord coord){
if(estVivante(coord)) return "#";
return ".";
}
private String showLig(int numLig){
String Lig = "";
for(int i = 0;i<nbCols;i++){
Coord coord = new Coord(numLig,i);
Lig = Lig + showCell(coord) + " ";
}
return Lig;
}
@Override
public String toString(){
String grilleJeu="";
String newLine = System.getProperty("line.separator");
for(int i = 0;i<this.nbLigs;i++){
grilleJeu = showLig(i) + newLine;
}
return grilleJeu;
}
public static void main (String args[]) {
Coord[] coords = {new Coord(0, 3), new Coord(1, 2), new Coord(2, 2), new Coord(2, 3), new Coord(2, 4)};
Game jeu = new Game(coords, 5, 5);
System.out.println(jeu);
}
} |
Coord.java
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
|
package vie;
class Coord {
int lig, col;
//constructeur
public Coord(int lig, int col){
this.lig = lig;
this.col = col;
}
@Override
public boolean equals(Object o){
if(this == (Coord) o) return true;
if(!(o instanceof Coord)) return false;
Coord x = (Coord) o;
return this.lig == x.lig && this.col == x.col;
}
@Override
public int hashCode() {
return String.format("%s,%s", this.lig, this.col).hashCode();
}
public static void main (String args[]) {
}
} |
Et quand je compile seulement pour afficher les cellules pour tester ce morceau(je suis au début) j'ai une erreur comme ça:
Citation:
run:
Exception in thread "main" java.lang.NullPointerException
at vie.Game.<init>(Game.java:14)
at vie.Game.main(Game.java:51)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Il semblerait que l'erreur soit à la ligne 14 donc celle ci:
Code:
this.damier.add(listeInitiale[i]);
Donc l'erreur dit qu'il reçoit un null et que ça devrait pas si je comprend bien,du coup un peu d'aide pour savoir pourquoi il n'y a rien dans mon tableau alors que je l'initialise bien dans mon main?
Merfi!