Java 出口罐元';不读图像

Java 出口罐元';不读图像,java,image,swing,nullpointerexception,Java,Image,Swing,Nullpointerexception,我正试图在JPanel中显示一个图像。以下是我正在使用的代码: URL imageURL; BufferedImage image = null; ImageIcon icon; imageURL = getClass().getClassLoader().getResource("images/audiorpglogo.png"); if (imageURL == null) { Syst

我正试图在JPanel中显示一个图像。以下是我正在使用的代码:

        URL imageURL;
        BufferedImage image = null;
        ImageIcon icon;

        imageURL = getClass().getClassLoader().getResource("images/audiorpglogo.png");
        if (imageURL == null) {
            System.out.println(imageURL == null);
            try { imageURL = new File("images/audiorpglogo.png").toURI().toURL(); }
            catch (Exception e1) { imageURL = null; }
        }

        System.out.println(imageURL == null);

        try { image = ImageIO.read(imageURL); }
        catch (Exception e) { }

        System.out.println(image == null);

        icon = new ImageIcon(image);

        System.out.println(icon == null);

        logo = new JLabel(icon);
在Eclipse中运行此程序时,我得到以下输出:

true
false
false
false
true
false
true
java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:204)
    at me.pogostick29.audiorpg.window.Window.<init>(Window.java:85)
    at me.pogostick29.audiorpg.window.WindowManager.setup(WindowManager.java:16)
    at me.pogostick29.audiorpg.AudioRPG.main(AudioRPG.java:30)
但是,当我将程序导出为可运行JAR时,我得到以下输出:

true
false
false
false
true
false
true
java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:204)
    at me.pogostick29.audiorpg.window.Window.<init>(Window.java:85)
    at me.pogostick29.audiorpg.window.WindowManager.setup(WindowManager.java:16)
    at me.pogostick29.audiorpg.AudioRPG.main(AudioRPG.java:30)
true
假的
真的
java.lang.NullPointerException
位于javax.swing.ImageIcon.(ImageIcon.java:204)
atme.pogostick29.audiorpg.window.window.(window.java:85)
at me.pogostick29.audiorpg.window.WindowManager.setup(WindowManager.java:16)
at me.pogostick29.audiorpg.audiorpg.main(audiorpg.java:30)
提前感谢您的帮助

请尝试以下代码:

try { 
  InputStream in = getClass().getResource(imgName); //Read from an input stream 
  img = new ImageIcon(ImageIO.read(in));    
}catch (Exception e1) {
 e1.printStackTrace();
} 
请尝试以下代码:

try { 
  InputStream in = getClass().getResource(imgName); //Read from an input stream 
  img = new ImageIcon(ImageIO.read(in));    
}catch (Exception e1) {
 e1.printStackTrace();
} 

您应该有一个名为
images
的资源文件夹,然后它就可以工作了

例如:

如何访问这些图标:

public BufferedImage icon32 = loadBufferedImage("/icon/icon32.png");
public BufferedImage icon64 = loadBufferedImage("/icon/icon64.png");

private BufferedImage loadBufferedImage(String string)
{
    try
    {
        BufferedImage bi = ImageIO.read(this.getClass().getResource(string));
        return bi;
    } catch (IOException e)
    {
        e.printStackTrace();
    }
    return null;
}

您应该有一个名为
images
的资源文件夹,然后它就可以工作了

例如:

如何访问这些图标:

public BufferedImage icon32 = loadBufferedImage("/icon/icon32.png");
public BufferedImage icon64 = loadBufferedImage("/icon/icon64.png");

private BufferedImage loadBufferedImage(String string)
{
    try
    {
        BufferedImage bi = ImageIO.read(this.getClass().getResource(string));
        return bi;
    } catch (IOException e)
    {
        e.printStackTrace();
    }
    return null;
}

更改了
imageURL=getClass().getClassLoader().getResource(“images/audiorpglogo.png”)
to
imageURL=getClass().getClassLoader().getResource(“/images/audiorpglogo.png”)并且它没有帮助。更改了
imageURL=getClass().getClassLoader().getResource(“images/audiorpglogo.png”)
to
imageURL=getClass().getClassLoader().getResource(“/images/audiorpglogo.png”)但它没有帮助。我这样做了,它起了作用,但现在我有了这个烂摊子:春季大扫除时间(代码版)!那我该怎么办?我是否先创建一个音频文件夹,然后创建一个locs子文件夹,然后将所有内容移入?(等等)是的,我会制作一个
audio
文件夹,然后把你所有的东西都放进去。它可能有很多(甚至有点杂乱无章),但是如果你用文件名按字母顺序对文件进行排序,你应该不会有问题。我这样做了,而且效果很好,但是现在我遇到了一个麻烦:春季大扫除(代码版)!那我该怎么办?我是否先创建一个音频文件夹,然后创建一个locs子文件夹,然后将所有内容移入?(等等)是的,我会制作一个
audio
文件夹,然后把你所有的东西都放进去。它可能有很多(甚至有点杂乱无章),但是如果您使用文件名按字母顺序对文件进行排序,您应该可以。