【问题标题】:Jar Embedded Resources NullPointerException [closed]Jar 嵌入式资源 NullPointerException [关闭]
【发布时间】:2012-11-27 14:14:56
【问题描述】:

我最初是从 Chillax 开始的,在临近截止日期遇到了这么多问题之后,我回到了我更熟悉的 IDE,NetBeans,并改变了我的方法,制作了一个更基本的“Asteroid”类游戏:

在 NetBeans 中我得到:

Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at gayme.Craft.<init>(Craft.java:27)
at gayme.Board.<init>(Board.java:54)
at gayme.Gayme.<init>(Gayme.java:9)
at gayme.Gayme.main(Gayme.java:19)
Java Result: 1

来源:(Craft 26 - 34)

    public Craft() {
    ImageIcon ii = new ImageIcon(this.getClass().getResource("craft.png"));
    image = ii.getImage();
    width = image.getWidth(null);
    height = image.getHeight(null);
    missiles = new ArrayList();
    visible = true;
    x = 40;
    y = 60;}

(板 54)

    craft = new Craft();

(Gayme 9)

    add(new Board());

(同性恋 19)

   new Gayme();

我确实有一些问题需要解决,而我睡眠不足的大脑在每一个问题上都出现了损失。随时为您喜欢的任何游戏提供帮助。 非常感谢大家!

【问题讨论】:

  • 在这两种情况下,您似乎都无法读取图像,可能它们在类路径或类似路径中丢失了

标签: java netbeans project embedded-resource


【解决方案1】:

有3种方法:

关于使用位于其中的 Jar 文件和资源需要记住的一些事项:

  • JVM 区分大小写,因此文件和包名称区分大小写。即主类位于 mypackage 我们现在无法使用如下路径提取它:myPackAge

  • 任何句点 '.'位于包名中的应替换为 '/'

  • 如果名称以“/”开头(“\u002f”),则资源的绝对名称是名称中“/”后面的部分。执行类时资源名以/开头,资源在不同的包中。

让我们使用我的首选方法getResource(..) 进行测试,该方法将返回我们资源的 URL:

我创建了一个包含 2 个包的项目:org.testmy.resources

如您所见,我的图片在 my.resources 中,而持有 main(..) 的 Main 类在 org.test 中。

Main.java:

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class Main {

    public static final String RES_PATH = "/my/resources";//as you can see we add / to the begining of the name and replace all periods with /
    public static final String FILENAME = "Test.jpg";//the case sensitive file name

    /*
     * This is our method which will use getResource to extarct a BufferedImage
     */
    public BufferedImage extractImageWithResource(String name) throws Exception {

        BufferedImage img = ImageIO.read(this.getClass().getResource(name));

        if (img == null) {
            throw new Exception("Input==null");
        } else {
            return img;
        }
    }

    public static void main(String[] args) {
        try {
            BufferedImage img = new Main().extractImageWithResource(RES_PATH + "/" + FILENAME);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}

如果您使用 RES_PATHFILENAME 的名称而不对实际文件进行适当的更改,则会出现异常(只是向您展示我们必须对路径有多小心)

更新:

对于您遇到的具体问题:

ImageIcon ii = new ImageIcon(this.getClass().getResource("craft.png"));

应该是:

ImageIcon ii = new ImageIcon(this.getClass().getResource("/resources/craft.png"));

Alien 和其他类也需要更改。

【讨论】:

  • @user1890890 测试了你的 netbeans 项目发现问题:查看更新
  • 感谢您的帮助。我更改了图像导入,现在它可以编译,但看起来像 this,然后立即进入“游戏结束”屏幕......很抱歉进一步打扰你,但有什么想法吗?
  • 您的图像没有被缩放,所以如果图像是 300x300,它将以 300x300 显示,请参阅我在此处缩放图像的答案:stackoverflow.com/questions/12660122/…
  • 天哪,我是个白痴……非常感谢!
  • 再次感谢,here 是我的游戏 zip,感谢您的帮助,功能齐全!
猜你喜欢
  • 2011-10-30
  • 1970-01-01
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多