1

我有一个HashMap<Integer, JButton>. 问题是当我尝试检索我得到的值时null,而不是 JButton。当我尝试在最后一行将“butt”添加到 centerPanel 时发生异常。下面是我的代码片段和 2 个类字段,用于透视代码。

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();
}  
}

为什么会发生 NullPointerException?我知道我不需要显式创建整数。

4

2 回答 2

1

这是一个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);
    }
}

输出是

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

我想现在你明白为什么你随机得到一个 NPE

于 2013-11-02T22:41:31.233 回答
1
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。然后你尝试centerPanel.add(butt);并获得一个 NPE。

于 2013-11-02T22:42:26.977 回答