L'exécution m'affiche le message d'erreur java.lang.NullPointerException
Bonjour,
Mon problème est que lorsque j’exécute mon programme, il m'affiche le message d'erreur suivant:
Exception in thread "main" java.lang.NullPointerException
at deuxmillesquarantehuit.init(deuxmillesquarantehuit.java:93)
at deuxmillesquarantehuit.jeu(deuxmillesquarantehuit.java:189)
at deuxmillesquarantehuit.<init>(deuxmillesquarantehuit.java:32)
at deuxmillesquarantehuit.main(deuxmillesquarantehuit.java:230)
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class deuxmillesquarantehuit extends JFrame implements KeyListener{
private static final long serialVersionUID = 1L;
private JLabel[][] casse;//=case
private JPanel grille;
private int[][] tabEnt;
private int nbLigneTab=4;
private int nbColonneTab=4;
public deuxmillesquarantehuit(){
this.setTitle("2048");
this.setSize(600, 800);
this.setLayout(new GridLayout(2,1));
this.setLocationRelativeTo(null);//Nous demandons maintenant à notre objet de se positionner au centre
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
grille();
jeu();
this.setVisible(true);
}
public Container grille()
{
grille = new JPanel();
grille.setSize(600, 600);
getContentPane().add(grille);
grille.setLayout(new GridLayout(4,4,2,2));
casse = new JLabel[4][4];
for(int i = 0 ; i < nbLigneTab ; i++)
{
for(int j = 0 ; j < nbColonneTab ; j++)
{
casse[i][j] = new JLabel("");
casse[i][j].setOpaque(true);
casse[i][j].setBackground(Color.white);
casse[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
casse[i][j].setFont(new Font("Arial", Font.BOLD, 50));
grille.add(casse[i][j]);
}
}
return grille;
}
public void init()
{
for(int i = 0 ; i < nbLigneTab ; i++)
{
for(int j = 0 ; j < nbColonneTab ; j++)
{
tabEnt[i][j]=0;
}
}
tabEnt[1][0]=2;
tabEnt[3][2]=4;
}
public void genererAleatoire()
{
boolean verif = (false);
int cpt=0;
do
{
long colAleatoire = Math.round(Math.random()*3); //random() renvoie un nombre aléatoire compris entre 0.0 et 1.0.
long ligneAleatoire = Math.round(Math.random()*3); //round() ajoute 0,5 à l'argument et restitue la plus grande valeur entière (int) inférieure ou égale au résultat
int col = (int)colAleatoire; //On parse
int ligne = (int)ligneAleatoire;
long nbAleatoire1 = Math.round(Math.random()*4);
int i = (int)nbAleatoire1;
while(verif == (false))
{
if(nbAleatoire1 == 0 || nbAleatoire1 == 1 || nbAleatoire1 == 3)
{
nbAleatoire1 = Math.round(Math.random()*4);
i = (int)nbAleatoire1;
}
else
verif = (true);
}
if (tabEnt[ligne][col] != 0)
{
verif = (false);
}
else
{
tabEnt[ligne][col] = i;
verif = (true);
}
}while(verif == (false) && cpt != 16);
}
public void incrementation()
{
for(int i = 0 ; i < nbLigneTab ; i++)
{
for(int j = 0 ; j < nbColonneTab ; j++)
{
if(tabEnt[i][j] == 0)
{
casse[i][j].setText("");
}
else
{
casse[i][j].setText( String.valueOf(tabEnt[i][j]));
}
}
}
}
public void jeu()
{
init();
incrementation();
}
public static void main(String[] args) {
new deuxmillesquarantehuit();
}
} |