简体   繁体   English

Class.getResource() 返回 null

[英]Class.getResource() returns null

I am trying to display pictures on the JPanel but I keep getting the error :我试图在 JPanel 上显示图片,但我不断收到错误消息:

java.lang.IllegalArgumentException: input == null! java.lang.IllegalArgumentException: 输入 == null!

I don't understand what is happening.我不明白发生了什么。

Here is the code I am using:这是我正在使用的代码:

public void actionPerformed(ActionEvent e) {
    try {
        Image image=ImageIO.read(getClass().getResource("img/" +num.getText()+".jpg"));

        Image resized = image.getScaledInstance(200, 200, 100);
        pictureFrame.setIcon(new ImageIcon(resized));
    } catch (Exception ex){
        ex.printStackTrace();
    }
}

This just leads to me getting the error!这只会导致我收到错误!

Stack trace produces the following:堆栈跟踪产生以下结果:

Java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1362)
    at work.Item.actionPerformed(Item.java:96)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6297)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
    at java.awt.Component.processEvent(Component.java:6062)
    at java.awt.Container.processEvent(Container.java:2039)
    at java.awt.Component.dispatchEventImpl(Component.java:4660)
    at java.awt.Container.dispatchEventImpl(Container.java:2097)
    at java.awt.Component.dispatchEvent(Component.java:4488)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
    at java.awt.Container.dispatchEventImpl(Container.java:2083)
    at java.awt.Window.dispatchEventImpl(Window.java:2489)
    at java.awt.Component.dispatchEvent(Component.java:4488)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:668)
    at java.awt.EventQueue.access$400(EventQueue.java:81)
    at java.awt.EventQueue$2.run(EventQueue.java:627)
    at java.awt.EventQueue$2.run(EventQueue.java:625)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:641)
    at java.awt.EventQueue$3.run(EventQueue.java:639)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:638)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

How can I solve this?我该如何解决这个问题? I have checked the location of the image, and have tried from different locations and always get the same error!我检查了图像的位置,并从不同的位置尝试过,总是得到相同的错误!

I'm using the Netbeans IDE.我正在使用 Netbeans IDE。

Assuming getClass() returns com.foo.bar.MyActionListener , getClass().getResource("img/foo.jpg") looks for a file named foo.jpg in the package com.foo.bar.img .假设getClass()返回com.foo.bar.MyActionListenergetClass().getResource("img/foo.jpg")在包com.foo.bar.img查找名为foo.jpg的文件。 If the image is not in this package, or if it is in this package but its root directory is not in the classpath, the method will return null.如果图像不在此包中,或者如果它在此包中但其根目录不在类路径中,则该方法将返回 null。

If the img folder is at the root of the classpath, you should use getClass().getResource("/img/foo.jpg") (note the leading / ), or getClass().getClassLoader().getResource("img/foo.jpg") .如果 img 文件夹位于类路径的根目录,则应使用getClass().getResource("/img/foo.jpg") (注意前导/ )或getClass().getClassLoader().getResource("img/foo.jpg")

You should give the relative path for your source file.您应该提供源文件的相对路径。 For exemple if you have this:例如,如果你有这个:

src
 --img
 --classes

And you are in the classes folder, you should write this:而你在classes文件夹中,你应该这样写:

getClass().getResource("../img/" +num.getText()+".jpg")

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

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