Board index » jbuilder » Problem With JBuilder Finding A Resource

Problem With JBuilder Finding A Resource


2004-09-28 12:25:08 PM
jbuilder24
I have an applet that should load a gif image as a resource, but the image
does not get loaded and I receive errors. What I end up with is an empty
applet and errors. Here is the code:
import java.applet.Applet;
import java.net.URL;
import java.awt.*;
import java.awt.image.ImageProducer;
public class ResourceImage extends Applet {
Image im;
public void start() {
URL url = getClass().getResource("globe.gif");
try {
im = createImage((ImageProducer)url.getContent());
if(im == null)
System.out.println("null image");
}
catch(Exception e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
Insets insets = getInsets();
g.drawImage(im, insets.left, insets.top, this);
}
public void update(Graphics g) {
paint(g);// this is just here so the background doesn't get
painted. globe.gif is an animated gif, with imageUpdate() constantly being
invoked.
}
}
The book I'm using, Graphic Java AWT 1.2 by Geary, states that the
getResource method delagates searching for the resource to the class loader,
and that if the resource is in the classpath it should be found. When
JBuilder executes this program, I get I get the following in the messages
window:
C:\JBuilderX\jdk1.4\bin\javaw -classpath
"C:\JBuilderPrograms\ResourceImage\classes;C:\JBuilderX\jd...followed by
many more directory paths.
The complete path to "globe.gif" is:
C:\JBuilderPrograms\ResourceImage\classes\globe.gif
Since the resource is in the classpath that JBuilder is using, why do I get
the following errrors?
java.lang.NullPointerException
at resourceimage.ResourceImage.start(ResourceImage.java:16)
at com.borland.jbuilder.runtime.applet.AppletTestbed.a(Unknown Source)
at com.borland.jbuilder.runtime.applet.AppletTestbed.startApplet(Unknown
Source)
at com.borland.jbuilder.runtime.applet.AppletTestbed.main(Unknown Source)
java.lang.NullPointerException
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:50)
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:736)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2755)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2745)
at resourceimage.ResourceImage.paint(ResourceImage.java:26)
at sun.awt.RepaintArea.paint(RepaintArea.java:177)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
at java.awt.Component.dispatchEventImpl(Component.java:3678)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
Or a better question is why isn't the image being loaded? I don't have a
CLASSPATH environment variable explicitly set on my machine (XP Pro sp2),
but am under the impression that the -classpath string in the java command
will establish all the search directories for the class loader. My next step
is to explicitly set a CLASSPATH in my system and see what happens.
Regardless of what THAT will produce, I still have the question here
concerning why JBuilder couldn't find the image?
 
 

Re:Problem With JBuilder Finding A Resource

In trying to track this problem down I surrounded the getResource() method
in start() with System.out.println as shown here. I reproduced the whole
program again, as it is short, it supercedes the original, and has more
information in the error messages. The class name is different because I
created a whole new project, ResourceImageTest, thinking that maybe the
problem had been a naming conflict with JBuilder (and ResourceImage seemed
like a likely candidate for that, without knowing any better). I'm using
JBuilder X Foundation.
package resourceimagetest;
import java.applet.Applet;
import java.net.URL;
import java.awt.*;
import java.awt.image.ImageProducer;
import java.awt.event.*;
public class ResourceImageTest extends Applet {
Image im;
public void start() {
System.out.println("In start()");
URL url = getClass().getResource("globe.gif");
System.out.println("URL: " + url);
try {
im = createImage((ImageProducer)url.getContent());
if(im == null)
System.out.println("null image");
}
catch(Exception e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
Insets insets = getInsets();
g.drawImage(im, insets.left, insets.top, this);
}
public void update(Graphics g) {
paint(g);
}
}
Now I get the following errors when I run the program, which yields a blank
applet. Notice the println outputs that come after the errors:
java.lang.NullPointerException
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:50)
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:736)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2755)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2745)
at resourceimagetest.ResourceImageTest.paint(ResourceImageTest.java:27)
at sun.awt.RepaintArea.paint(RepaintArea.java:177)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
at java.awt.Component.dispatchEventImpl(Component.java:3678)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
In start()
URL:
file:/C:/JBuilderPrograms/ResourceImageTest/classes/resourceimagetest/globe.gif
So, "globe.gif" is found when I put it in under the package directory (URL
was null if I put the image under the classes directory). Why all the errors
before even the start() method is entered?
 

Re:Problem With JBuilder Finding A Resource

Ah, I found the problem. I changed the start() method to init() and the
program worked. This is something peculiar to JBuilder, because I changed
the init() method back to start() and ran the program with my other IDEs
(textpad, IE, and the java executable that comes with JDK (I've got 1.5.0-RC
installed, though with JBuilder I'm using the default 1.4.2 that comes with
it. I should have caught this sooner because I had the same problem a couple
of weeks ago with a Label not showing up until I removed the instantiating
code from the start() method and put it in the init() method instead. I
suppose this isn't a big problem, as it makes more sense to put
instantiating code in the init() method anyway. I was using the code from
the book and didn't connect this to my earlier problem. Never again will
this one get me.
 

{smallsort}