ImageIcon a;URL url = getClass().getResource("image/1.gif");
a = new ImageIcon(url);
button = new JButton( a );
第二句语句“a = new ImageIcon(url);”编译出错··为什么啊?

解决方案 »

  1.   

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)这样的错误
      

  2.   

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at netlianliankan.Game_One_Panel.PaiLie(Game_One_Panel.java:88)
    at netlianliankan.Game_One_Panel.<init>(Game_One_Panel.java:42)
    at netlianliankan.NetLianLianKanPanel.<init>(NetLianLianKanPanel.java:27)
    at netlianliankan.NetLianLianKanMain.<init>(NetLianLianKanMain.java:40)
    at netlianliankan.NetLianLianKanMain.main(NetLianLianKanMain.java:149)
    这样的错误
      

  3.   


    new ImageIcon参数是String类型啊。你搞错了啊
      

  4.   

    我想将url获得的图片显示在button中,应该怎样写啊?谢谢·
      

  5.   

    Public ImageIcon(URL url)
    可以的
      

  6.   

    ImageIcon a=null;
    试试吧
      

  7.   

    import java.awt.BorderLayout;
    import java.awt.Rectangle;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    public class TestJPanel extends JFrame{
        private JPanel jpanel;
        private ImageIcon imageIcon;
        private JLabel imageLabel;
        public TestJPanel(){
           imageIcon = new ImageIcon("main.png"); //写入文件路径
           imageLabel = new JLabel(imageIcon);         //初始化JLabel
           jpanel = new JPanel();
           this.add(BorderLayout.NORTH,jpanel);
           this.setVisible(true);  //设置为显示
           this.pack();
           //设置jframe的大小
           this.setBounds(new Rectangle(100,100,imageIcon.getIconWidth(),imageIcon.getIconHeight()));  
           jpanel.add(BorderLayout.CENTER,imageLabel);
         } 
        public static void main(String[] args){
           TestJPanel testJPanel = new TestJPanel();
         }
    }
     
    你参照一下
      

  8.   

    依然是这些错误!
    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at netlianliankan.Game_One_Panel.PaiLie(Game_One_Panel.java:88)
    at netlianliankan.Game_One_Panel.<init>(Game_One_Panel.java:42)
    at netlianliankan.NetLianLianKanPanel.<init>(NetLianLianKanPanel.java:27)
    at netlianliankan.NetLianLianKanMain.<init>(NetLianLianKanMain.java:40)
    at netlianliankan.NetLianLianKanMain.main(NetLianLianKanMain.java:149)
      

  9.   

    D:\JAVA\jdk1.6.0_26\lib 下有这个文件啊
      

  10.   

    URL url = getClass().getResource("image/1.gif");
    改成
    URL url = getClass().getResource("/image/1.gif");
    这个可以吧,感觉是路径的问题,导致url = null ,执行第二句a = new ImageIcon(url);
    的时候抛出了空指针异常。
      

  11.   

    输出一下url出不出错,如果出错了,那就是图片路径不对,看看图片的目录,和图片的名字,图片的后缀有没有问题,
      

  12.   

    1.gif放在src/image/1.gif 下面的吗?你可以debug一下得到的url=null吧,
      

  13.   

    package com.cj;import java.net.URL;import javax.swing.ImageIcon;public class Testjj {
    ImageIcon a;
    /**
     * @param args
     */
    public void test()
    {
    URL url =this.getClass().getResource("/image/1.gif");
    a = new ImageIcon(url);
    System.out.println("ksksk");
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Testjj a = new Testjj();
    a.test();
    }}
    1.gif放在src/image/1.gif  没有问题啊
      

  14.   

    真不想回答你!!
    你还没搞清楚学java最基本的东西,然后就开始研究这些华丽的图片了
    这样反而会耽搁更多的时间!!
    ------
    你那是异常,不是编译问题
    ==========================
    YouClass.class.getClassLoader().getResource("image/1.gif");
    image目录放在eclipse的src目录里面,这样打成jar包也能用!
    ==========================
    一个步子迈太大,出了问题你根本就解决不了,就算有人帮,估计也够呛
    把这个东西做完,去看swing之前的东西吧,异常、流什么的,别整swing了!
      

  15.   

    是啊!!你说得太对了,我就是打包成jar文件,图片全都不见了,所以就改成那样子了!!呵呵··我基础确实很差,因为我java是自学的··什么都是自己在摸索,没有人教!不懂的时候就在这里发发帖···请原谅!我在src新建了image ,但是我不知道怎样讲图片放进image里··