这是什么原因啊?
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:138)
at UserCardLayoutFrame.<init>(UserCardLayoutFrame.java:40)
at UserCardLayoutFrame.main(UserCardLayoutFrame.java:89)
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class UserCardLayoutFrame extends JFrame{
private JTextField txtId; // 声明文本框
private URL url = null; // 获得图片的URL
private Icon icon = null;               // 创建图象对象
private JPanel imgPanel; // 声明面板
private CardLayout cardLayout; // 声明卡片布局
public UserCardLayoutFrame(){
setTitle("使用卡片布局浏览照片"); // 设置窗体标题
setBounds(100, 100, 365, 252); // 设置窗体的显示位置和大小
// 设置窗体的默认关闭方式为关闭并退出应用程序
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final JPanel northPanel = new JPanel(); // 创建面板
getContentPane().add(northPanel, BorderLayout.NORTH); // 在窗体容器的北部(即上面)位置添加面板容器
final JLabel label = new JLabel(); // 创建标签
label.setText("请输入照片编号:"); // 设置标签显示的文字
northPanel.add(label); // 将标签添加到面板上
txtId = new JTextField(10); // 创建文本框
northPanel.add(txtId); // 将文本框添加到面板上
imgPanel=new JPanel(); // 创建面板
cardLayout=new CardLayout(); // 创建卡片布局
imgPanel.setLayout(cardLayout); // 将面板设置为卡片布局
getContentPane().add(imgPanel,BorderLayout.CENTER); // 将面板添加到窗体容器的中间位置
for (int i=1;i<=6;i++){
//URL url = UserCardLayoutFrame.class.getResource("/image/"+i+".gif"); // 获得图片的URL
ImageIcon icon = new ImageIcon(UserCardLayoutFrame.class.getResource("/image/1.gif"));   // 创建图象对象
//Image image = icon.getImage();
imgPanel.add(String.valueOf(i),new JLabel(icon)); // 将带图标的标签添加到面板上
}
final JButton button = new JButton(); // 创建按钮
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
String id=txtId.getText(); // 从文本框中获得图片的id
cardLayout.show(imgPanel, id); // 通过图片id在卡片布局的面板中显示图片
}
});
button.setText("显示照片"); // 设置按钮的标题
northPanel.add(button); // 将按钮添加到面板上
final JPanel southPanel = new JPanel(); // 创建面板
getContentPane().add(southPanel, BorderLayout.SOUTH); // 将面板添加到窗体的南部(即底部)位置
final JButton dButton = new JButton(); // 创建按钮
dButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
cardLayout.first(imgPanel); // 显示容器中的第一个组件
}
});
dButton.setText("第一张"); // 设置按钮的标题
southPanel.add(dButton); // 将按钮添加到面板上
final JButton fButton = new JButton(); // 创建按钮
fButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
cardLayout.previous(imgPanel); // 显示容器中的第一个组件
}
});
fButton.setText("上一张"); // 设置按钮的标题
southPanel.add(fButton); // 将按钮添加到面板上
final JButton gButton = new JButton(); // 创建按钮
gButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
cardLayout.next(imgPanel); // 显示容器中的第一个组件
}
});
gButton.setText("下一张"); // 设置按钮的标题
southPanel.add(gButton); // 将按钮添加到面板上
final JButton button_1 = new JButton(); // 创建按钮
button_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
cardLayout.last(imgPanel); // 显示容器中的第一个组件
}
});
button_1.setText("最后一张"); // 设置按钮的标题
southPanel.add(button_1); // 将按钮添加到面板上
}
public static void main(String[] args) {
UserCardLayoutFrame frame=new UserCardLayoutFrame(); // 创建窗体对象
frame.setVisible(true); // 显示窗体
}
}

解决方案 »

  1.   

    应该是路径问题。
    在 eclipse里的话 把 image目录的父目录添加的Build Path.在命令行的,把 image目录的父目录 附加到  -classpath 里。
      

  2.   

    at javax.swing.ImageIcon.<init>(ImageIcon.java:138)

    应该是图片加载失败
    不行就设置绝对路径试试吧
      

  3.   

    java.lang.NullPointerException
    你有句话没有取到值  ,null值进行操作 就会这样的 ,可以加个条件判断 , 具体哪句就要调试看看了  ,建议加try catch 调试
      

  4.   

    at javax.swing.ImageIcon.<init>(ImageIcon.java:138)应该是图片加载失败
    不行就设置绝对路径试试吧
    试过了不行。报错在这行ImageIcon icon = new ImageIcon(UserCardLayoutFrame.class.getResource("/image/1.gif")); // 创建图象对象
      

  5.   

    说说我的愚解,不对的地方请大虾们指点下,问题还是路径问题。windows系统的资源名格式应该是"\\",不是“/”。比如:D:\\image\1.gif    File file = new File("D:\\image\\1.gif");
        System.out.println(file.exists());通过以上验证是可以取到该文件的。但是将绝对路径替换到你的程序当中,还是取不到文件。ImageIcon icon = new ImageIcon(UserCardLayoutFrame.class.getResource("D:\\image\\1.gif")); // 创建图象对象中文API中的解释:
    如果 name 以 '/' ('\u002f') 开始,则绝对资源名是 '/' 后面的 name 的一部分。 
    否则,绝对名具有以下形式: 
       modified_package_name/name
    其中 modified_package_name 是此对象的包名,该名用 '/' 取代了 '.' ('\u002e')。 因为路径是不以“/”开始,所以都会默认为modified_package_name/name。可以将image\1.gif文件复制到包路径下就可以获取到图片了。ImageIcon icon = new ImageIcon(UserCardLayoutFrame.class.getResource("\\image\\1.gif")); // 创建图象对象或ImageIcon icon = new ImageIcon(UserCardLayoutFrame.class.getResource("image\\1.gif")); // 创建图象对象