Class.getResource() returns null
Asked Answered
S

2

14

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.

Sulamith answered 22/3, 2013 at 23:59 Comment(16)
getClass().getResource("img/" +num.getText()+".jpg") returns null. Returns: A URL object or null if no resource with this name is foundTahitian
according to your post, it should be easy to understand what's the problem by using debugging modeTincher
Is your current working directory not what you expect it to be? Also, have you logged out "img/"+num.getText()+".jpg" to make sure it's what you expect?Recreation
@Tincher using debugging mode this what happens. as soon as line 96 is read getClass().getResource("img/" +num.getText()+".jpg" it throws the exception?Sulamith
@tjameson i put the images folder in the src where all the .java files and gave error. Put outside src and also gave error.. what's the problem?Sulamith
@Tahitian yes it always returns null even though the image is in the folderSulamith
@Sulamith - Your current working directory isn't where the source is, it's where the class files or jar files are built. This might help you debug: #4871551Recreation
@tjameson: the current directory doesn't have anything to do with the problem. The OP loads the image using the class loader.Hobie
have you watch what "img/" +num.getText()+".jpg" returns ? Then check if the resource does exist. The problem should come because the JVM is not executed in the right folder so your app can't find your resource. You can try this String path = new java.io.File(".").getCanonicalPath(); to check the current directory.Tincher
@tjameson i looked at the thread posted and once running the code this is the output: Current dir using System:C:\Documents and Settings\Administrator\My Documents\Work no mention of src/images as this is where the image i want is storedSulamith
@Tincher doesn't allow me to see what it returns, throws an exception straight away.. the image is in the image folderSulamith
-1 Please take the time to isolate the problem first.Perquisite
@user2201158: you keep saying that the image is in the image folder, but you never say where the image folder is. That's the key problem. Read my answer.Hobie
@Sulamith I don't know which IDE are you using, but in eclipse you have the "watch" to see what it returns. By the way you can first create a string String = "img/" +num.getText()+".jpg", then display it to be sure that you have indeed the resourceTincher
@Tincher using netbeans IDE 7.2Sulamith
@Sulamith I don't use this IDE so I can't help, but JB Nizet made the right answer to solve your problem.Tincher
H
29

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

Hobie answered 23/3, 2013 at 0:10 Comment(12)
how do I get this to work.. do i try by renaming the image i have in my images folder to foo? or just try the code given?Sulamith
No. You make sure to put the img folder in the right location. Or you change the path used to access the image so that it finds it where it is. Without saying where you put the img folder, I can't say which path you should use.Hobie
the images folder is currently inside the src folder under this path C:\Documents and Settings\Administrator\My Documents\WorkSulamith
So you should use what I tell you to use at the end of my answer: getClass().getResource("/img/foo.jpg") or getClass().getClassLoader().getResource("img/foo.jpg"). Of course, you must replace foo by the value returned by num.getText(): getClass().getResource("/img/" + num.getText() + ".jpg")Hobie
ok i have something like this: String a = num.getText(); Image img=ImageIO.read(getClass().getClassLoader().getResource("img/"+a+".jpg"));Sulamith
And? What happens? What is printed when you do System.out.println("::" + a + "::");? Assuming ::foo:: is printed, and assuming your IDE puts the compiled class files in the build directory, does the file build/img/foo.jpg exist?Hobie
System.out.println("::" + a + "::") prints out a as expected but going to build/img/a.jpg no fileSulamith
Then your IDE, or your build procedure, doesn't copy the non .java files from the source directory to the build directory. IDEs, AFAIK, all do that by default. Is it a basic NetBeans project, or is it a Maven or Gradle project? Are you sure it prints ::a:: and not :: a:: or ::a :: (note the white spaces)?Hobie
ok didnt put into build directory but put into a directory called classes. now if i copy images folder into that directory it worksSulamith
You shouldn't put anything in that directory. This directory is the target directory where the IDE compiles the files. It will be deleted as soon as you clean build your project. But AFAIK, NetBeans should copy non Java files in the source directory to this classes directory aspart of the compilation. Unless you're not using a basic NetBeans project (but Maven or Gradle), or you use a custom build script to build your project, or I'm wrong. Anyway, you must configure your build procedure in order to copy these image files to the classes directory.Hobie
is there a way to get it working without putting it into this directory called classes? can't i have it so it loads from images folder in the src directory? Thats the only way its working.... i just attempted what you said about clean and build and yes it gets deleted so what can i do to get around this.. the project is just a normal java applicationSulamith
this saves me, thanks.Wingspan
O
0

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")
Overdrive answered 17/2, 2015 at 19:1 Comment(1)
Do not do this. This won't work in all cases. (Fails e.g. for jar files.)Chibcha

© 2022 - 2024 — McMap. All rights reserved.