Jar 嵌入式资源 NullPointerException [英] Jar Embedded Resources NullPointerException

查看:15
本文介绍了Jar 嵌入式资源 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初是从 Chillax 开始的,在临近截止日期遇到这么多问题之后,我又回到了我更熟悉的 IDE NetBeans,并将我的方法更改为更基本的Asteroid"类型游戏:

I originally started with Chillax, after Encountering so many problems so near to deadline, I went back to the IDE I am more familiar with, NetBeans, and I changed my approach to a more basic "Asteroid"-type game:

在 NetBeans 中我得到:

In NetBeans I get:

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

来源:(工艺 26 - 34)

sources: (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();

(同性恋 9)

    add(new Board());

(同性恋 19)

   new Gayme();

我有一些真正需要解决的问题,而我睡眠不足的大脑在每个问题上都遇到了问题.随时为您喜欢的任何游戏提供帮助.非常感谢各位!

I have issues that I really need resolved and my sleep deprived brain is coming up with a loss on each. Feel free to help out on whichever game you'd rather. Thanks so much guys!

推荐答案

有3种方法:

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

A few things to remember about working with Jar files and resources located within:

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

  • JVM is case sensitive thus file and package names are case sensitive. i.e Main class is located within mypackage we cannot now extract it with a path like: myPackAge

任何句点."位于包名内的应替换为/"

Any period '.' located within the package name should be replaced by '/'

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

If the name begins with a '/' ('u002f'), then the absolute name of the resource is the portion of the name following the '/'. Resource names begin with / when executing class and the resources are in different packages.

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

Lets put that to the test using my preferred method of getResource(..) which will return the URL of our resource:

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

I create a project with 2 packages: org.test and my.resources:

如您所见,我的图像在 my.resources 中,而包含 main(..) 的 Main 类在 org.test 中>.

As you can see my image is in my.resources while the Main class which hold main(..) is in 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 的名称而不对实际文件进行适当的更改,您将得到一个异常(只是向您展示我们必须多么小心带路径)

If you play around with the names of the RES_PATH or FILENAME without making appropriate changes to actual files you will get an exception (just shows yous how careful we must be with paths)

更新:

对于您的具体问题:

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

应该是:

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

Alien等类也需要修改.

这篇关于Jar 嵌入式资源 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆