pb avec les tableaux

pb avec les tableaux - Java - Programmation

Marsh Posté le 19-03-2003 à 13:51:11    

J'ai une classe Screen :  
 

Citation :


package ecran;
public class Screen {
 private char matrix[];
 
 public Screen()
 {
   for (int i=1 ; i < 40 ; i++)  
  {
   matrix[i] = 0;
  }
 }
 
}


 
et qd je run ca me met :

Citation :

java.lang.NullPointerException
 at ecran.Screen.<init>(Screen.java:9)


 
apparement ca viens du tableau....

Reply

Marsh Posté le 19-03-2003 à 13:51:11   

Reply

Marsh Posté le 19-03-2003 à 14:29:05    

Oui, t'as pas initialisé ton tableau. Tu le déclares mais tu ne l'initialises pas.
Donc pour que ça marche, il faudrait écrire juste avant ton for : matrix = new char[40]; (puisqu'apparemment tu veux une longueur de 40).
Pour info, un tableau en Java c'est considéré comme un objet (j'ai appris ça y a pas très longtemps, surtout passke j'faisais pas gaffe ?3 ce que j'écrivais [:ddr555] ) donc il faut l'initialiser avec un new. De plus, tu as aussi accès au nombre d'éléments du tableau avec le champ length que tu appelles en faisant matrix.length.
Vala. Mate un peu la doc de Java avant de te lancer direct dans la prog : http://developer.java.sun.com/deve [...] /new2java/
 
EDIT : http://java.sun.com/docs/books/jls [...] C.doc.html c'est bien aussi :)


Message édité par Taiche le 19-03-2003 à 14:31:29

---------------
Everyone thinks of changing the world, but no one thinks of changing himself  |  It is the peculiar quality of a fool to perceive the faults of others and to forget his own  |  Early clumsiness is not a verdict, it’s an essential ingredient.
Reply

Marsh Posté le 19-03-2003 à 14:46:11    

[:cupra]

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed