Monday, April 12, 2010

Java NullPointerException: Uncaught error fetching image

These days I'm working with Java, Swing & AWT. An interesting move from web development. Now I can catch many errors at runtime and Java usually points to where the problem is, except for this exception I came across recently:


Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:97)
at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:106)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)


None of my classes are mentioned there!

I was trying to add some new icons to the application as part of an animation. For this purpose I have a class called AnimatedIcon which implements Icon.
One of the parameters in the contructor is the path to the image file. An AnimatedIcon object is created for each icon in the animation so I have something like this:


for(int i1 = 0; i1 < frames; i1++) {
    String filename=fileset+(i1+1)+".gif";
    Image tmp=java.awt.Toolkit.getDefaultToolkit().getImage(getClass().getResource(filename));
    images[i1]=new ImageBuffer(tmp);
}



I added a debug output to the loop to try and see what was going on and I saw this in the console;

DEBUG 2010-04-08 13:01:16,615 [AWT-EventQueue-0] ImageManager: /test/app/images/eventenvelope DEBUG 2010-04-08 13:01:16,615 [AWT-EventQueue-0] AnimatedIcon: /test/app/images/eventenvelope1.gif Uncaught error fetching image: java.lang.NullPointerException at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:97) at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:106) at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240) at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:17) at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
DEBUG 2010-04-08 13:01:16,616 [AWT-EventQueue-0] AnimatedIcon: /test/app/images/eventenvelope2.gif DEBUG 2010-04-08 13:01:16,616 [AWT-EventQueue-0] AnimatedIcon: /test/app/images/eventenvelope3.gif DEBUG 2010-04-08 13:01:16,616 [AWT-EventQueue-0] AnimatedIcon: /test/app/images/eventenvelope4.gif DEBUG 2010-04-08 13:01:16,618 [AWT-EventQueue-0] AnimatedIcon: /test/app/images/eventenvelope5.gif DEBUG 2010-04-08 13:01:16,618 [AWT-EventQueue-0] AnimatedIcon: /test/app/images/eventenvelope6.gif DEBUG 2010-04-08 13:01:16,618 [AWT-EventQueue-0] AnimatedIcon: /test/app/images/eventenvelope7.gif

From this I assumed that there was something wrong with eventenvelope1.gif so I tried replacing it and even renaming evenlope2.gif to envelope1.gif however it always threw the exception on the first image. It turned out that Eclipse just couldn't find the file. I needed to refresh the images folder in Eclipse. After doing this the error went away!

I was debugging this for ages so I hope this post will save time for someone. It's quite simple to re-produce the error, just delete one of the images. It is a shame the error isn't a bit more descriptive.