Reputation: 1614
I have a HashMap<Integer, JButton>
. The problem is when I try to retrieve a value I get null
, not a JButton. The exception occurs when I try to add "butt" to centerPanel in the last line. Below is a snippet of my code an 2 class fields to put the code into perspective.
public class GUI {
private JPanel centerPanel;
private JButton button;
private JLabel label;
private Image source;
private Image image;
private HashMap<Integer, JButton> images = new HashMap<>();
public GUI() {
centerPanel = new JPanel();
ImageIcon sid = new ImageIcon(GUI.class.getResource("koala.jpg"));
source = sid.getImage();
int ind = 0;
for ( int i = 0; i < 4; i++) {
for ( int j = 0; j < 3; j++) {
if ( j == 2 && i == 3) {
label = new JLabel("");
centerPanel.add(label);
} else {
button = new JButton();
button.addActionListener(this);
images.put(new Integer(++ind), button);
image = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(j*width/3, i*height/4,
(width/3)+1, height/4)));
button.setIcon(new ImageIcon(image));
}
}
}
Random random = new Random();
for (int i=0; i<11; i++) {
Integer numb = new Integer(random.nextInt(images.size()));
JButton butt = images.get(1);
centerPanel.add(butt);
images.remove(numb);
}
setSize(1024, 768);
setTitle("Puzzle");
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
}
Why does the NullPointerException occur? I'm aware I don't need to explicitly create Integers.
Upvotes: 1
Views: 511
Reputation: 20015
Integer numb = new Integer(random.nextInt(images.size()));
JButton butt = images.get(1);
centerPanel.add(butt);
images.remove(numb);
On one iteration numb
will be equal to 1 (this can occur on any iteration, as you retrieve a random number between 0 and images.size(), and this is guaranteed to occur, since images.size()
decreases from 11 to 1), so element under index 1 will be removed. On next iteration images.get(1)
returns null
. Then you attempt centerPanel.add(butt);
and get an NPE.
Upvotes: 1
Reputation: 17595
Here is a SSCCE
@Test
public void mapNPE() {
Map<Integer, String> images = new HashMap<>();
int ind = 0;
for ( int i = 0; i < 4; i++) {
for ( int j = 0; j < 3; j++) {
if ( j == 2 && i == 3) {
System.out.println("j == 2 && i == 3");
} else {
images.put(new Integer(++ind), Integer.toString(i) + "," + Integer.toString(j));
}
}
}
Random random = new Random();
for (int i=0; i<11; i++) {
Integer numb = new Integer(random.nextInt(images.size()));
System.out.println(numb);
if(numb == 1) {
System.out.println("Image will be removed, next iteration will get null from map");
}
String butt = images.get(1);
System.out.println(Integer.toString(i) + "=" + butt);
images.remove(numb);
}
}
and the output is
j == 2 && i == 3
4
0=0,0
3
1=0,0
3
2=0,0
8
3=0,0
5
4=0,0
1
Image will be removed, next iteration will get null from map
5=0,0
5
6=null
3
7=null
1
Image will be removed, next iteration will get null from map
8=null
4
9=null
5
10=null
I guess now you get why you randomly get a NPE
Upvotes: 1