-4

I am trying to load and draw it with paint method in java whatever the way I write the path it always shows an exception

java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)

I have the image at the same folder with the class

This is the line that I am loading image in

    Image img = ImageIO.read(getClass().getResourceAsStream("pepsi.png"));
soorapadman
  • 4,451
  • 7
  • 35
  • 47
Amado
  • 371
  • 1
  • 16
  • Did you check this [link](https://stackoverflow.com/questions/15424834/java-lang-illegalargumentexception-input-null-when-using-imageio-read-to-lo) – soorapadman Jun 19 '19 at 06:55
  • Yes and I am still getting the same exception I tried every solution written there – Amado Jun 19 '19 at 06:58
  • Are you sure the file is in the same place as the java file? could be in another class folder, put it in the same folder as the class **this** code runs. – ilightwas Jun 19 '19 at 07:12
  • Yes they are at the same place – Amado Jun 19 '19 at 07:37

3 Answers3

1

Have a look at MKYong's tutorial. It shows you where to put your image. If you want the image to be loaded as "resource", you have to put it in the resources folder. You project structure would be like this:

MyProject
    +--src
        +--main
            +--java
            |    +-com
            |       +--me
            |           +--Main.java
            +--resources
                 +--pepsi.jpg

and in your Main class you execute that snippet:

try {
    Image img= ImageIO.read(Main.class.getClassLoader().getResourceAsStream("pepsi.jpg"));
    System.out.println(img.getWidth(null));  //this is just a test, when it prints out the width of your image, you have the right file loaded 
} catch (IOException ex) {
    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
GameDroids
  • 5,584
  • 6
  • 40
  • 59
0

check your output folder... the 'pepsi.png' (maybe case sensitive) must be in same folder as the class file your calling from

0

Try to test it like this.

If the file is by the java file and my class is in a package called

net.ilightwas.MyClass

it should be like

Image img = ImageIO.read(getClass().getClassLoader().getResourceAsStream("net/ilightwas/pepsi.png"));

This will probably help you find the mistake.

ilightwas
  • 152
  • 7