我試圖創建我的小盒子,顯示從組合框中選擇顏色。但是當我嘗試運行該程序時,我不斷收到NullPointerException的這個錯誤。我沒有看到它有什麼問題。NullPointerException,殺死我的程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ThreeColorsFrame extends JFrame
{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 400;
private JComboBox box;
private JLabel picture;
private static String[] filename = { "Red", "Blue", "Green" };
private Icon[] pics = { new ImageIcon(getClass().getResource(filename[0])),
new ImageIcon(getClass().getResource(filename[1])),
new ImageIcon(getClass().getResource(filename[2])) };
public ThreeColorsFrame()
{
super("ThreeColorsFrame");
setLayout(new FlowLayout());
box = new JComboBox(filename);
box.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent event)
{
if (event.getStateChange() == ItemEvent.SELECTED)
picture.setIcon(pics[box.getSelectedIndex()]);
}
});
add(box);
picture = new JLabel(pics[0]);
add(picture);
}
}
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at ThreeColorsFrame.<init>(ThreeColorsFrame.java:33)
at ThreeColorsViewer.main(ThreeColorsViewer.java:36)
安置自己的錯誤堆棧跟蹤 –
*「NullPointerException異常,當我嘗試運行該程序」 *複製/粘貼堆棧跟蹤的[編輯該問題](http://stackoverflow.com/posts/15998813/edit)。 –