Here is the error I'm getting

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!

And here is the code

 try
            {           
                //myPicture = ImageIO.read(new File("Companylogo_FINAL.jpg"));
              myPicture = ImageIO.read(getClass().getResource("/resources/Companylogo_FINAL.jpg"));
                ImageIcon img = new ImageIcon(myPicture);
                frame.setIconImage(img.getImage());
            } 
            catch (IOException e1)
            {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


            JLabel lbl = new JLabel(new ImageIcon(myPicture));

As you can see I have changed this line

myPicture = ImageIO.read(new File("Companylogo_FINAL.jpg"));

To this

 myPicture = ImageIO.read(getClass().getResource("/resources/Companylogo_FINAL.jpg"));

As by earlier code I was getting error "cannot read file"

Dani AI

Generated

That IllegalArgumentException means ImageIO.read(...) received a null input. In other words, getResource(...) did not find the file and returned null, which matches what suspected about the classpath. ’s suggestion (dropping the leading slash) only works when the image sits in the same package as the calling class. Because you are in the default package, relative lookups are fragile; prefer an absolute resource path that starts with /, and make sure the file is actually on the runtime classpath.

Quick, reliable workflow (works in a jar too):

  1. Put the image on the classpath. Create a resources/ folder alongside your compiled classes and configure your build/IDE to copy it to the output (OP later confirmed the manual copy was the issue). After build, you should have .../classes/resources/Companylogo_FINAL.jpg.

  2. Load with an absolute path and fail fast if missing.

URL url = MyClass.class.getResource("/resources/logo.jpg");
if (url == null) {
    throw new IllegalStateException("Missing resource: /resources/logo.jpg");
}
BufferedImage img = ImageIO.read(url);
frame.setIconImage(img);                 // only after img is non-null
JLabel label = new JLabel(new ImageIcon(img));

Alternatively, use a stream (best when running from a jar):

try (InputStream in = MyClass.class.getResourceAsStream("/resources/logo.jpg")) {
    if (in == null) throw new IllegalStateException("Missing resource");
    BufferedImage img = ImageIO.read(in);
    // use img...
}

Tips:

  • Resource lookups are case-sensitive on most OSes once packaged. Check the exact filename and extension.
  • Avoid new File(...) for bundled resources; it breaks after packaging, as noted by .
  • Consider adding a real package to your classes; it makes relative resource paths predictable.

Recommended Answers

All 11 Replies

Try this

myPicture = ImageIO.read(getClass().getResource("Companylogo_FINAL.jpg"));

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!

Same error

Exactly which line does the error message point to?

It points to

myPicture = ImageIO.read(getClass().getResource("/resources/Companylogo_FINAL.jpg"));

Almost certainly a problem with the path to the jpeg (assuming the jpeg itself is OK). That form of reference works well and requires a "resouces" folder to be in the classpath - best alongside the folder for the outermost package where the class files are stored. Sorry if that's confusing wording.
Eg
your .java file begins

package myApp;

class myClass {
   ...

Your run-time folder structure should look like

<some folder in the classpath>
   myApp
      myClass.class
   resources
      logo.jpg
1.Classfile folder
2.Images Folder >Companylogo_FINAL.jpg.

Don't you have any package specified for your code?

No Pakage specified,Just Default.
EDIT:
Please someone expalain ,it is confusing to me.
Now I have tried this code which was giving me error earlier(Cannot read file)...now works.

myPicture = ImageIO.read(new File("Companylogo_FINAL.jpg"));

At least not giving me error on that line anymore.

99% of problems like this come down to Java looking for the file in the wrong place. (or at least not the place you expected). Check out the File API doc for a full description of how partial paths are completed.

The getResource solution is better (if you can make it work) because that will continue to work when you package your code and resources in a jar file.

Yes that is the better solution.
But the problem is still there with that code.(Still working on that).
Meanwhile another errors to take care off.

Now I have solved the porblem with my Image from my resource.

I was doing import the wrong way..manually copy and past the image into the folder(Wrong way).
Correct way is to use import in the file then import>your folder name where you want to import it and done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.