Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class.getResource() returns null

I am trying to display pictures on the JPanel but I keep getting the error :

java.lang.IllegalArgumentException: input == 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.

like image 470
user2201158 Avatar asked Mar 22 '13 23:03

user2201158


People also ask

Why ClassLoader getResource returns null?

I suspect the issue is that you do not have the file in the classpath. Show activity on this post. Instead of having the resource file in the same folder as your source files, create a resources folder parallel to the java source folder.

What does class getResource do?

The getResource() method of java Class class is used to return the resources of the module in which this class exists. The value returned from this function exists in the form of the object of the URL class.

Why does getResourceasstream return null?

If the path of the file is the same of yout class (but under resources as main dir) you can just put the name of the file without any / . For example if your class has the following path src/main/java/paths/Lifepaths. java , your file has to have this path src/main/resources/paths/Lifepaths.

How do I get resources in Java?

Java programs can use two mechanisms to access resources: Applets use Applet. getCodeBase() to get the base URL for the applet code and then extend the base URL with a relative path to load the desired resource, for example with Applet. getAudioClip(url) .


2 Answers

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. 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.

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").

like image 105
JB Nizet Avatar answered Sep 23 '22 23:09

JB Nizet


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:

getClass().getResource("../img/" +num.getText()+".jpg")
like image 44
Omar Chaabouni Avatar answered Sep 23 '22 23:09

Omar Chaabouni