Jar Embedded Resources NullPointerException [英] Jar Embedded Resources NullPointerException

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

问题描述

我最初是从Chillax开始的,在遇到如此多的问题接近截止日期之后,我回到了我更熟悉的IDE,NetBeans,并且我改变了我的方法,以更基本的小行星型游戏:

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;}

(Board 54)

    craft = new Craft();

(Gayme 9)

    add(new Board());

(Gayme 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种方法:

  • Class#getResourceAsStream(String name)
  • Class#getResource(String name)
  • ToolKit#createImage(URL url)

关于使用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.test my.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:

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_PATH FILENAME 的名称,而不对实际文件进行适当的更改,您将获得异常(只是显示我们对路径的谨慎程度)

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 和其他类还需要改变。

The Alien and other classes also need to be changed.

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

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