Problème NullPOinterException dans un tableau perso

Résolu
joebar3333 Messages postés 41 Date d'inscription dimanche 24 octobre 2004 Statut Membre Dernière intervention 29 juin 2005 - 23 mai 2005 à 19:07
cs_GodConan Messages postés 2113 Date d'inscription samedi 8 novembre 2003 Statut Contributeur Dernière intervention 6 octobre 2012 - 23 mai 2005 à 20:18
salut,

voila je veux faire un tableau composé d'un type perso. Le problème est que je n'arrive pas à l'initialiser correctement.
Voila mon source :

public class monTab {

class monInfo{
boolean selected;
Color color;
}

private monInfo[] tabCartes;
private int typeOfCarte;

public monTab(int size, int type) {
tabCartes= new monInfo[size];
for(int i = 0; i < tabCartes.length; i++)
{
tabCartes[i].selected = false;
tabCartes[i].colo = Color.BLUE;
}
typeOfCarte = type;
}
}

Donc l'erreur se produit dans le constructeur de monTab, lorsque je veux affecter des valeurs à tabCartes. J'initialise mal tabCartes aparamment, mais je ne sais pas comment y rémédier.
Aidez moi please.

@+

1 réponse

cs_GodConan Messages postés 2113 Date d'inscription samedi 8 novembre 2003 Statut Contributeur Dernière intervention 6 octobre 2012 12
23 mai 2005 à 20:18
public monTab(int size, int type)
{
tabCartes= new monInfo[size];
for(int i = 0; i < tabCartes.length; i++)
{
//////////////////////
tabCartes[i] = new monInfo();
// indispensable d obtenir une reference valide avant d utilisé un objet... ;o)

tabCartes[i].selected = false;
tabCartes[i].colo = Color.BLUE;
}
typeOfCarte = type;
}

GL

++
GodConan
3
Rejoignez-nous