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.

Community
  • 1
  • 1
user2201158
  • 153
  • 1
  • 2
  • 8
  • 2
    `getClass().getResource("img/" +num.getText()+".jpg")` returns `null`. *Returns: A URL object or null if no resource with this name is found* – Esailija Mar 23 '13 at 00:00
  • according to your post, it should be easy to understand what's the problem by using debugging mode – Tako Mar 23 '13 at 00:02
  • 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? – beatgammit Mar 23 '13 at 00:05
  • @Tako using debugging mode this what happens. as soon as line 96 is read getClass().getResource("img/" +num.getText()+".jpg" it throws the exception? – user2201158 Mar 23 '13 at 00:06
  • 1
    @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? – user2201158 Mar 23 '13 at 00:08
  • @Esailija yes it always returns null even though the image is in the folder – user2201158 Mar 23 '13 at 00:08
  • @user2201158 - 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: http://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java – beatgammit Mar 23 '13 at 00:08
  • @tjameson: the current directory doesn't have anything to do with the problem. The OP loads the image using the class loader. – JB Nizet Mar 23 '13 at 00:11
  • 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. – Tako Mar 23 '13 at 00:12
  • 1
    @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 stored – user2201158 Mar 23 '13 at 00:12
  • @Tako doesn't allow me to see what it returns, throws an exception straight away.. the image is in the image folder – user2201158 Mar 23 '13 at 00:15
  • -1 Please take the time to **isolate the problem** first. –  Mar 23 '13 at 00:16
  • @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. – JB Nizet Mar 23 '13 at 00:18
  • @user2201158 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 resource – Tako Mar 23 '13 at 00:24
  • @Tako using netbeans IDE 7.2 – user2201158 Mar 23 '13 at 00:27
  • @user2201158 I don't use this IDE so I can't help, but JB Nizet made the right answer to solve your problem. – Tako Mar 23 '13 at 00:29

2 Answers2

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

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 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? – user2201158 Mar 23 '13 at 00:19
  • 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. – JB Nizet Mar 23 '13 at 00:21
  • the images folder is currently inside the src folder under this path C:\Documents and Settings\Administrator\My Documents\Work – user2201158 Mar 23 '13 at 00:24
  • 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")` – JB Nizet Mar 23 '13 at 00:28
  • ok i have something like this: String a = num.getText(); Image img=ImageIO.read(getClass().getClassLoader().getResource("img/"+a+".jpg")); – user2201158 Mar 23 '13 at 00:37
  • 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? – JB Nizet Mar 23 '13 at 00:41
  • System.out.println("::" + a + "::") prints out a as expected but going to build/img/a.jpg no file – user2201158 Mar 23 '13 at 00:49
  • 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)? – JB Nizet Mar 23 '13 at 00:54
  • ok didnt put into build directory but put into a directory called classes. now if i copy images folder into that directory it works – user2201158 Mar 23 '13 at 00:55
  • 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. – JB Nizet Mar 23 '13 at 01:00
  • 1
    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 application – user2201158 Mar 23 '13 at 01:01
  • this saves me, thanks. – Lucas Oct 11 '22 at 01:54
1

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")
Omar Chaabouni
  • 448
  • 2
  • 19