错误:未捕获的错误获取图像:java.lang.NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10681257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:05:13  来源:igfitidea点击:

error : Uncaught error fetching image: java.lang.NullPointerException

javaimageswing

提问by Raj

The image path is all correct only, I don't know why this error is coming. Any one have any idea?

仅图像路径全部正确,我不知道为什么会出现此错误。任何人有任何想法?

Code is for putting a background image in a frame and add button over that image

代码用于将背景图像放入框架中并在该图像上添加按钮

The error is

错误是

Uncaught error fetching image:
    java.lang.NullPointerException
        at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:99)
        at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:113)
        at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240)
        at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
        at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)

Code

代码

Error is coming while running the code :

运行代码时出现错误:

        public class BackgroundImg extends JPanel {

            private Image img;

          public BackgroundImg (String img)
          {
              this(new ImageIcon(img).getImage());
          }

          public BackgroundImg (Image img)
          {
            this.img = img;
            Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
            setPreferredSize(size);
            setMinimumSize(size);
            setMaximumSize(size);
            setSize(size);
            //setLayout(null);
          }

            @Override
          public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
            g.drawImage(img, 0, 0, null);
          }
        }

        public class ApplicationFrame {

        public static void main(String[] args) throws ImageAccessException {
        BackgroundImg panel = new BackgroundImg(Toolkit.getDefaultToolkit().createImage(ApplicationFrame.class.getResource("C:\test.png")));
    JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(800, 600);

                frame.getContentPane().add(panel);
                ApplicationContents.addContentsToPane(panel);

                frame.setVisible(true);
                //   frame.setVisible(true);

            }

        }


    }
public class ApplicationContents {
    public static void addContentsToPane(Container pane)
    {
        //pane.setLayout(null);
        pane.setLayout(new FlowLayout());

        JButton b1 = new JButton("Test");

        pane.add(b1);

        //Insets insets = pane.getInsets();
        //Dimension size = new Dimension(120,32);

        // b1.setBounds(0 + insets.left, 0 + insets.top,
         //            size.width, size.height);


         b1.addActionListener(
                 new ActionListener()
         {
             public void actionPerformed(ActionEvent event)
            {

                 JOptionPane.showMessageDialog(null, "Test" , "Test", JOptionPane.ERROR_MESSAGE);
            }
         }
         );
    }

}

回答by eis

getResource() loads a resource from classpath, not an OS path, so even if "C:\test.png" would be correct you cannot load it that way.

getResource() 从类路径加载资源,而不是操作系统路径,因此即使 "C:\test.png" 是正确的,您也无法以这种方式加载它。

Also, please check the return value of getResource() before using it so you'd catch these kinds of errors.

另外,请在使用 getResource() 之前检查它的返回值,以便您可以捕获这些类型的错误。

You might want to bundle the image as a resource within your jar and load it using path that specifies the location within your jar.

您可能希望将图像捆绑为 jar 中的资源,并使用指定 jar 中位置的路径加载它。

回答by MaVRoSCy

You have to use Toolkit.getDefaultToolkit().createImage("c:\\tmp\\test.png");to get the image from a static folder, or if you want to include the image inside your jar, set the image path relatively to the .class file.

你必须使用Toolkit.getDefaultToolkit().createImage("c:\\tmp\\test.png"); 要从静态文件夹中获取图像,或者如果要将图像包含在 jar 中,请设置相对于 .class 文件的图像路径。