Re: Bug in code from Java Tutorial or is it just me?
I changed the code to look for the JPG and re-compiled and ran it.
OK. But more importantly, did you also change the code
to report any problems with loading the image (as my first
example did)? Your earlier statements indicated you were
not entirely sure where to put the image so it could be
I saw exactly the same output as you on first run.
The NPE. As soon as I spotted that shoddy use of
a String to indicate a file (better to go with a File or
URL, IMO..), I thought it worth putting the extra stack
trace dump in (and especially since I could not be
bothered supplying an image).
and.. well shucks, now it has got to this, I am
compelled to ..
- copy over a GIF
- change the code to reference it
- run the code to see..
- A JFrame titled 'Image Label' with the GIF inside
and 'Java 2D Graphics!' printed over the top. The GIF
was smaller than the frame content area, and was horribly
stretched (as might be expected when doing an image
resize using Graphics.drawImage() to resize it) and did
not cycle (it was an animated GIF).
So - this supports my theory that the code is not finding
the resource. Perhaps you should look into the File/URL
methods of specifying the image.
Note that for my simplistic test, I removed the package
statement and put the image in the same directory as
the code (and used a String reference).
Message posted via http://www.javakb.com