0
public void loadStdImage() throws IOException
{
    Image image = ImageIO.read(this.getClass().getResource("/Resources/Images/Student/Capture.png"));  //Line 350
    ImageIcon icon = new ImageIcon(image);
    JLabel lblImage = new JLabel(icon);
    lblImage.setIcon(icon);
    lblImage.setBounds(753, 50, 149, 171);
    add(lblImage);
}

I tried many things... but nothing works out. Continuously showing the following run-time error

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at View.Student.loadStdImage(Student.java:350)

Project folder structure is:

Project Explorer view in Eclipse for a project

edit: Found the solution. See the change of icon of the resource folder in the following picture and the above image. I added my resource folder to Java Build Path. Right click on your project, go to properties, then select 'Java Build Path', from there add your folder to java build path. Cheers enter image description here

ONE_FE
  • 968
  • 3
  • 19
  • 39
  • 2
    Check the path of the named resource and make sure it's (correct) and within reach of the classpath. What IDE are you using? – MadProgrammer Dec 25 '15 at 06:34
  • double checked the path.... no joy, i'm usng ecllipse – ONE_FE Dec 25 '15 at 06:39
  • 1
    So the `Resources` directory should either be in the project directory or the src directory. Remember the paths are case sensitive and typically `Resources` is called `resources` ;) – MadProgrammer Dec 25 '15 at 06:42
  • The Resources directory is in the same level as src directory.... mine 'Resources' – ONE_FE Dec 25 '15 at 06:50
  • 1
    The only reason you would get a `null` input, is because the resource doesn't exist at the location you are specifying (or the spelling/case is wrong) – MadProgrammer Dec 25 '15 at 07:02
  • Spelling is right... I double checked it again... – ONE_FE Dec 25 '15 at 07:12
  • 1
    Do a new build (clean and build if possible), export the project as a Jar, unzip the Jar and verify that the images you are looking for are where you expect them to be within the Jar – MadProgrammer Dec 25 '15 at 07:15
  • Thank you very much, I got the answer – ONE_FE Dec 26 '15 at 05:38

1 Answers1

3

welcome to SO. As you are new here, please read this - https://stackoverflow.com/help/mcve

Let me help you with this for now.

I have standard Eclipse project:

Eclipse project

and my test class looks like (minimal):

package q34460547;

import java.awt.Image;
import java.io.IOException;

import javax.imageio.ImageIO;

public class LoadTest {

    public static void main(String[] args) throws IOException {
        new LoadTest().loadStdImage();
    }

    public void loadStdImage() throws IOException {
        Image image = ImageIO.read(this.getClass().getResource("/ScreenShot005.png"));
    }

}

and now, when I used

ImageIO.read(this.getClass().getResource("/ScreenShot005.png"));

image is loaded from res so called source folder in Eclipse.

When I used

ImageIO.read(this.getClass().getResource("ScreenShot005.png"));

image s loaded from the folder in which LoadTest.java file is (to be precise it is also compiled to same folder - in Eclipse it's bin).

You can find more info for example here - What is the difference between Class.getResource() and ClassLoader.getResource()?

edit:

The image has to be on classpath (when using Class.getResource), that's why it was not loaded from Resources folder. There are two options, use another version of ImageIO.read() or make your Resources folder a source folder:

Project properties - source folders in Eclipse

Community
  • 1
  • 1
Betlista
  • 10,327
  • 13
  • 69
  • 110
  • This is very useful. Finally found it. Thank You – ONE_FE Dec 26 '15 at 05:37
  • it's working when the image is in the same folder as the .java class. But when the image is in the Resource folder (Find the file hierachy in this link: https://drive.google.com/file/d/0B9bLkMQp6h6SX09nNFdUeklNZzA/view?usp=sharing) I suggest the code should be as like this: ImageIO.read(this.getClass().getResource("/Resources/Images/Student/Capture.PNG")); But i'm getting a ' java.lang.IllegalArgumentException' – ONE_FE Dec 26 '15 at 06:51
  • Notice the difference in folder icon for my `src` (same as your `src`) or my `img`, but your `Resources` folder has different icon - see the edit ;-) Please edit your question and add the picture there, it might help others in future ;-) – Betlista Dec 26 '15 at 07:47