Erreur Java !!
Résolu
cbnla
Messages postés
4
Statut
Membre
-
cbnla Messages postés 4 Statut Membre -
cbnla Messages postés 4 Statut Membre -
Bonjour,
J'ai essayé de rouler mon programme java bien que ça compile bien mais ça m'affiche aprés l'avoir roulé quelque chose du genre :
Je sais pas vraiment comment résoudre ce problème et j'ai vraiment besoin d'aide !
voici mon code :
Merci !!
J'ai essayé de rouler mon programme java bien que ça compile bien mais ça m'affiche aprés l'avoir roulé quelque chose du genre :
Exception in thread "main" java.lang.NullPointerException
at Cell.setIcon(App.java:30)
at Cell.<init>(App.java:25)
at App.<init>(App.java:46)
at App.main(App.java:58)
Je sais pas vraiment comment résoudre ce problème et j'ai vraiment besoin d'aide !
voici mon code :
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
class Cell extends JButton {
private static final int NUM_COLOURS = 3;
private ImageIcon[] icons;
private int type;
public Cell() {
ImageIcon[] icons;
icons = new ImageIcon[NUM_COLOURS];
for (int i=0; i<NUM_COLOURS; i++) {
icons[i] = new ImageIcon("data/ball-" + Integer.toString(type) + ".png");
}
this.type = 0;
setBackground(Color.WHITE);
setIcon();
setBorderPainted(false);
}
private void setIcon() {
setIcon(icons[type]);
}
public void update() {
type = type + 1;
setIcon();
}
}
public class App extends JFrame implements ActionListener {
private Cell myCell;
public App() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myCell = new Cell();
myCell.addActionListener(this);
add(myCell);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
myCell.update();
}
public static void main(String[] args) {
new App();
}
}
Merci !!
1 réponse
-
Bonjour,
L'erreur te donne plusieurs informations, à commencer par le type d'erreur en elle mêmeNullPointerException
, et laligne de code sur laquelle ça s'est produitat Cell.setIcon(App.java:30)
. Donc tu peux aller voir la documentation de l'erreur et regarder à quoi ça correspond dans le code.
private void setIcon() { setIcon(icons[type]); // ligne 30 : NullPointerException }
https://docs.oracle.com/javase/9/docs/api/java/lang/NullPointerException.htmlThrown when an application attempts to use null in a case where an object is required. These include:
- Calling the instance method of a null object.
- Accessing or modifying the field of a null object.
- Taking the length of null as if it were an array.
- Accessing or modifying the slots of null as if it were an array.
- Throwing null as if it were a Throwable value.
Ici, c'est tonprivate ImageIcon[] icons;
(ligne 12) qui vaut null.
Tu pensais l'avoir initialisé ligne 18 avecicons = new ImageIcon[NUM_COLOURS];
mais comme tu as déclaré une variable locale juste avant (ImageIcon[] icons;
ligne 16), alors c'est cette variable locale qui est initialisée, pas l'attribut privé.
Il faudrait que tu supprimes la variable locale ligne 16, elle ne sert à rien, ce dont tu as besoin c'est uniquement de l'attribut privé de la ligne 12...