Java HashMap上的NullPointerException
我有一个Java HashMap上的NullPointerException,java,nullpointerexception,hashmap,Java,Nullpointerexception,Hashmap,我有一个HashMap。问题是当我试图检索一个值时,我得到的是null,而不是JButton。当我尝试在最后一行的中心面板中添加“butt”时,会出现异常。下面是我的代码片段,包含两个类字段,用于将代码放入透视图中 public class GUI { private JPanel centerPanel; private JButton button; private JLabel label; private Image source; private Image image; priva
HashMap
。问题是当我试图检索一个值时,我得到的是null
,而不是JButton。当我尝试在最后一行的中心面板中添加“butt”时,会出现异常。下面是我的代码片段,包含两个类字段,用于将代码放入透视图中
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();
}
}
公共类GUI{
私人JPanel中心面板;
私人按钮;
私人标签;
私有图像源;
私有图像;
私有HashMap图像=新HashMap();
公共图形用户界面(){
centerPanel=新的JPanel();
ImageIcon sid=newImageIcon(GUI.class.getResource(“koala.jpg”);
source=sid.getImage();
int ind=0;
对于(int i=0;i<4;i++){
对于(int j=0;j<3;j++){
如果(j==2&&i==3){
标签=新的JLabel(“”);
添加(标签);
}否则{
按钮=新的JButton();
addActionListener(这个);
put(新整数(++ind),按钮);
image=createImage(新的FilteredImageSource(source.getSource()),
新CropImageFilter(j*宽度/3,i*高度/4,
(宽/3)+1,高/4);
按钮。设置图标(新图像图标(图像));
}
}
}
随机=新随机();
对于(inti=0;i,这里是一个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);
}
}
我想现在你明白为什么你随机获得NPE了
Integer numb = new Integer(random.nextInt(images.size()));
JButton butt = images.get(1);
centerPanel.add(butt);
images.remove(numb);
在一次迭代中,numb
将等于1(这可能发生在任何迭代中,因为您检索到的是介于0和images.size()之间的随机数,并且这是保证发生的,因为images.size()
从11减少到1),所以索引1下的元素将被删除。在下一次迭代中,images.get(1)
返回null
。然后尝试中心面板。添加(对接);
并获得NPE。在ind
和图像之间是否有任何关系。size()
?提供一个SSCCE。使用发布的代码,我们只能猜测。并打印存储在映射中的键,以及尝试从映射中获取值时使用的键,以查看可能的错误。还要检查映射在两个位置是否是同一对象。跟踪是您的朋友。调试器是您最好的朋友。是的。每当我将映射放入图像时,我想images.size也会变小吗?当你看到一个名为“butt”的变量时,还有人傻笑吗好的,我可以看到不完整的代码会导致一些问题。我包括了整个类。是的,我明白了。这是一个多么愚蠢的错误。我刚刚将代码从ArrayList翻译成HashMap,还以为我还在处理索引。这是一个多么愚蠢的错误=/
Integer numb = new Integer(random.nextInt(images.size()));
JButton butt = images.get(1);
centerPanel.add(butt);
images.remove(numb);