Java
sarah9999
Messages postés
27
Statut
Membre
-
Pacorabanix Messages postés 4122 Date d'inscription Statut Membre Dernière intervention -
Pacorabanix Messages postés 4122 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
j'ai veut faire un programme qui simule une mer de poisson alors j'ai fais 03 classes
Poisson; Mer qui est une matrice[x][y] de Poisson et la classe Main;
lorsque je veux affichier la marice des poisson ça n'a pas marché et voilà mes codes:
public class Poisson
{
int age;
char poisson;
int positionX,positionY;
public Poisson(int positionX,int positionY,char poisson,int age)
{
this.positionX=positionX;
this.positionY=positionY;
this.poisson=poisson;
this.age=age;
}
public char getPoisson()
{
return poisson;
}
public String toString()//marche avec essai
{
String chaine="";
//if poisson!=null then
return chaine+getPoisson();
//else return chaine;
}
public void affichePoisson()//marche avec essai
{
System.out.println(this.toString());
}
}
************************fin class poisson
public class Mer
{
public Poisson[][] mer ;
public Mer(int dimX,int dimY)
{
mer=new Poisson[dimX][dimY];
for (int x=0;x<=mer.length-1;x++)
for (int y=0;y<=mer.length-1;y++)
this.mer[x][y]=null;
}
public void logerPoisson(int positionX,int positionY,Poisson poisson)
{
mer[positionX][positionY]=poisson;
}
public void afficheMer(Mer mer)
{
/*for (int x =0;x<=this.mer.length-1;x++)
for (int y=0;y<=this.mer[x].length-1;y++)
*
* //affiche poisson a x et y */
System.out.println(mer);//avec cette facon elle affiche une adresse hexa
System.out.printlen(mer[x][y].toString()); //declenche une erreur à l'exce nullpointerexception
}
***********************fin class mer
public static void main(String[] args)
{
// TODO code application logic here
Mer uneMer=new Mer(20,20);
Poisson p1=new Poisson(1,1,'r',0);
uneMer.logerPoisson(1,1,p1);
Poisson p2=new Poisson(2,2,'s',0);
uneMer.logerPoisson(2,2,p2);
Poisson p3=new Poisson(3,3,'r',0);
uneMer.logerPoisson(3,3,p3);
Poisson p4=new Poisson(4,4,'s',0);
uneMer.logerPoisson(4,4,p4);
p1.affichePoisson();//l'affichage des poisson marche tres bien
p2.affichePoisson();
p3.affichePoisson();
p4.affichePoisson();
uneMer.afficheMer(uneMer);//****A CE NIVEAU EST LE PROBLEME
}
SVP s'il ya quelq'un qui me corrige?et merci
j'ai veut faire un programme qui simule une mer de poisson alors j'ai fais 03 classes
Poisson; Mer qui est une matrice[x][y] de Poisson et la classe Main;
lorsque je veux affichier la marice des poisson ça n'a pas marché et voilà mes codes:
public class Poisson
{
int age;
char poisson;
int positionX,positionY;
public Poisson(int positionX,int positionY,char poisson,int age)
{
this.positionX=positionX;
this.positionY=positionY;
this.poisson=poisson;
this.age=age;
}
public char getPoisson()
{
return poisson;
}
public String toString()//marche avec essai
{
String chaine="";
//if poisson!=null then
return chaine+getPoisson();
//else return chaine;
}
public void affichePoisson()//marche avec essai
{
System.out.println(this.toString());
}
}
************************fin class poisson
public class Mer
{
public Poisson[][] mer ;
public Mer(int dimX,int dimY)
{
mer=new Poisson[dimX][dimY];
for (int x=0;x<=mer.length-1;x++)
for (int y=0;y<=mer.length-1;y++)
this.mer[x][y]=null;
}
public void logerPoisson(int positionX,int positionY,Poisson poisson)
{
mer[positionX][positionY]=poisson;
}
public void afficheMer(Mer mer)
{
/*for (int x =0;x<=this.mer.length-1;x++)
for (int y=0;y<=this.mer[x].length-1;y++)
*
* //affiche poisson a x et y */
System.out.println(mer);//avec cette facon elle affiche une adresse hexa
System.out.printlen(mer[x][y].toString()); //declenche une erreur à l'exce nullpointerexception
}
***********************fin class mer
public static void main(String[] args)
{
// TODO code application logic here
Mer uneMer=new Mer(20,20);
Poisson p1=new Poisson(1,1,'r',0);
uneMer.logerPoisson(1,1,p1);
Poisson p2=new Poisson(2,2,'s',0);
uneMer.logerPoisson(2,2,p2);
Poisson p3=new Poisson(3,3,'r',0);
uneMer.logerPoisson(3,3,p3);
Poisson p4=new Poisson(4,4,'s',0);
uneMer.logerPoisson(4,4,p4);
p1.affichePoisson();//l'affichage des poisson marche tres bien
p2.affichePoisson();
p3.affichePoisson();
p4.affichePoisson();
uneMer.afficheMer(uneMer);//****A CE NIVEAU EST LE PROBLEME
}
SVP s'il ya quelq'un qui me corrige?et merci
A voir également:
- Java
- Jeux java itel - Télécharger - Jeux vidéo
- Waptrick java football - Télécharger - Jeux vidéo
- Waptrick java voiture - Télécharger - Jeux vidéo
- Java apk - Télécharger - Langages
- Eclipse java - Télécharger - Langages
2 réponses
Bonjour Sarah9999,
Ceci s'explique :
car ceci :
Quand ta cellule this.mer[x][y] est null, aucune méthode ne peut être appliquée puisqu'il n'y a pas d'objet.
Donc, avant de faire tes System.out.println, il faut que tu testes si un objet a été instancié dans cette cellule de ton tableau.
Ceci s'explique :
System.out.println(mer); //avec cette facon elle affiche une adresse hexa System.out.printlen(mer[x][y].toString()); //declenche une erreur à l'exce nullpointerexception
car ceci :
for (int x=0;x<=mer.length-1;x++)
for (int y=0;y<=mer.length-1;y++)
this.mer[x][y]=null;
Quand ta cellule this.mer[x][y] est null, aucune méthode ne peut être appliquée puisqu'il n'y a pas d'objet.
Donc, avant de faire tes System.out.println, il faut que tu testes si un objet a été instancié dans cette cellule de ton tableau.
public void afficheMer(Mer mer)
{
for (int x =0;x<=this.mer.length-1;x++)
for (int y=0;y<=this.mer[x].length-1;y++)
if mer[x][y] !=null //A CE NIVEAU IL YA UNE ERREUR QUI DIT ARRAY REQUIRED BUT MER FOUND
ET SA DECLENCHE AUSSI UNE AUTRE l ERREUR A UNE LIGNE QUI MARCHE BIEN AVANT CETTE MODIFICATION DONT VOILA SON CODE (System.out.println(this.mer[x][y]);
)
//affiche poisson a x et y
//System.out.println( this.toString(x,y,mer[x][y]));
//test des referencethis.toStringssi elles existent aprés creation
//System.out.println((mer[x][y]));il faut un this pourquelle marche sinon
//sa plonte
System.out.println(this.mer[x][y]);/------erreur aussi/
-------------------MERCI DE M AIDER CAR J AI PASSE BCP DU TEMPS POUR CE PROGRAMME-----------