简体   繁体   English

getClass() getResource 返回 null

[英]getClass() getResource returning null

When I try and load a image from a resource directory in eclipse I keep getting a null pointer exception (NPE).当我尝试从 eclipse 中的资源目录加载图像时,我不断收到空指针异常 (NPE)。 The res folder is in the project directory. res 文件夹位于项目目录中。 This where I get the NPE:这是我获得 NPE 的地方:

image.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("res/bg.jpg")).getImage().getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_SMOOTH)));

When I remove the getClass().getReource() the image is returned:当我删除 getClass().getReource() 图像返回:

image.setIcon(new ImageIcon(new ImageIcon(("res/bg1.jpg")).getImage().getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_SMOOTH)));

When I print the URL for the res directory I get null:当我打印 res 目录的 URL 时,我得到空值:

URL resource = this.getClass().getResource("/");
        resource.getFile(); // print me somehwere

        URL resource1 = this.getClass().getResource("/res");
        resource.getFile(); // print me as well

        URL resource2 = this.getClass().getResource("res/bg2");
        resource.getFile(); // print me as well

        System.out.println("Reource1 : " + resource);
        System.out.println("Reource1 : " + resource1);
        System.out.println("Reource1 : " + resource2);

Output:输出:

Reource1 :file:/C:/Users/cmooney/eclipse-workspace/TextSimplifier/bin/资源 1 :file:/C:/Users/cmooney/eclipse-workspace/TextSimplifier/bin/
Reource1 : null资源 1:空
Reource1 : null资源 1:空

I have refreshed, cleaned, and built the project several times.我已经多次刷新、清理和构建该项目。

Directory:目录: 在此处输入图片说明

Any idea why this is happening?知道为什么会这样吗?

Thanks谢谢

From the official java doc: https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResource-java.lang.String-来自官方 Java 文档: https : //docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResource-java.lang.String-

Finds a resource with a given name.查找具有给定名称的资源。 The rules for searching resources associated with a given class are implemented by the defining class loader of the class.搜索与给定类相关联的资源的规则由类的定义类加载器实现。 This method delegates to this object's class loader.此方法委托给此对象的类加载器。 If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResource(java.lang.String).如果此对象由引导类加载器加载,则该方法委托给 ClassLoader.getSystemResource(java.lang.String)。
Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:在委托之前,使用以下算法从给定的资源名称构造一个绝对资源名称:

  • If the name begins with a '/' ('\/'), then the absolute name of the resource is the portion of the name following the '/'.如果名称以“/”(“\/”)开头,则资源的绝对名称是“/”后面的名称部分。

  • Otherwise, the absolute name is of the following form:否则,绝对名称采用以下形式:
    modified_package_name/name修改包名称/名称
    Where the modified_package_name is the package name of this object with '/' substituted for '.'其中 modified_pa​​ckage_name 是此对象的包名称,其中 '/' 替换了 '.' ('\.'). ('\.')。

Replace this.getClass().getResource("res/bg.jpg") with this.getClass().getResource("../res/bg.jpg")this.getClass().getResource("res/bg.jpg")替换为this.getClass().getResource("res/bg.jpg") this.getClass().getResource("../res/bg.jpg")

Since this.getClass().getResource("/") returns file:/C:/Users/cmooney/eclipse-workspace/TextSimplifier/bin/ , you need to go one level(directory) up in the directory structure so that you can enter the res directory.由于this.getClass().getResource("/")返回file:/C:/Users/cmooney/eclipse-workspace/TextSimplifier/bin/ ,您需要在目录结构中向上一级(目录),以便您可以进入res目录。 It's like cd ../res from the current location of file:/C:/Users/cmooney/eclipse-workspace/TextSimplifier/bin/它就像cd ../resfile:/C:/Users/cmooney/eclipse-workspace/TextSimplifier/bin/的当前位置file:/C:/Users/cmooney/eclipse-workspace/TextSimplifier/bin/

Note : I can't see bg.jpg in the screenshot that you have attached.注意:我在您附加的屏幕截图bg.jpg不到bg.jpg Make sure, you have bg.jpg in this path.确保您在此路径中有bg.jpg

I think, you got the directory structure wrong.我想,你弄错了目录结构。 If '/' points to the bin directory, as shown by the output of your first resource, '/res' points to the res subdirectory of bin and not to the sibling in the parent directory of bin.如果“/”指向 bin 目录,如第一个资源的输出所示,“/res”指向 bin 的 res 子目录,而不是 bin 父目录中的同级目录。

You need to either move the res directory or change the way the resolution of '/' works.您需要移动 res 目录或更改“/”的分辨率工作方式。

The rules for searching resources associated with a given class are implemented by the defining class loader of the class.搜索与给定类相关联的资源的规则由类的定义类加载器实现。

from Java documentation来自Java 文档

For maven based projects the resource directory is usually src/main/resources.对于基于 maven 的项目,资源目录通常是 src/main/resources。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM