Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help with NullPointerException

10 views
Skip to first unread message

Daniel Hollenbeck

unread,
Dec 7, 1996, 3:00:00 AM12/7/96
to

Hey All,

I keep getting a NullPointerException when I try to load
an image in my application.


C:\classes>java ImageApplet
java.lang.NullPointerException
at java.applet.Applet.getDocumentBase(Applet.java:59)
at ImageApplet.init(ImageApplet.java:14)
at ImageApplet.main(ImageApplet.java:44)


My simple test code is below.

Any ideas what I am doing wrong?

Thanks in advance.

Dan
holle...@mailbag.com


import java.awt.*;
import java.applet.*;

public class ImageApplet extends Applet
{
Image logo;
private MediaTracker tracker;

public void init()
{
tracker = new MediaTracker (this);
logo = getImage (getDocumentBase (), "logo.gif");

try
{
tracker.waitForAll ();
}
catch (InterruptedException e)
{
return;
}
setBackground (Color.black);
}

public void paint(Graphics g)
{
int width = logo.getWidth(this);
int height = logo.getHeight(this);

g.drawRect(52, 52, width+10, height + 10);
g.drawImage(logo, 57, 57, width, height, this);
}

public static void main(String args[])
{
ImageApplet app = new ImageApplet();
Frame frame = new Frame("Hello world!");

app.init();
app.start();

frame.add("Center", app);
frame.resize(400,400);
frame.show();
}
}

Mark Wutka

unread,
Dec 8, 1996, 3:00:00 AM12/8/96
to

Daniel Hollenbeck (holle...@mailbag.com) wrote:

: I keep getting a NullPointerException when I try to load


: an image in my application.


: C:\classes>java ImageApplet
: java.lang.NullPointerException
: at java.applet.Applet.getDocumentBase(Applet.java:59)
: at ImageApplet.init(ImageApplet.java:14)
: at ImageApplet.main(ImageApplet.java:44)


: public void init()


: {
: tracker = new MediaTracker (this);
: logo = getImage (getDocumentBase (), "logo.gif");

: try
: {
: tracker.waitForAll ();
: }

It may be upset because you forgot to add the image to the
media tracker. You need a call like:
tracker.addImage(logo, 0);
after your getImage.

Mark
--
_________________________________________________________
Mark Wutka wu...@netcom.com
Author of "Hacking Java: The Java Professional's Resource Kit"
ISBN: 0-7897-0935-X (formerly titled "Java Expert Solutions")

Doug Bell

unread,
Dec 8, 1996, 3:00:00 AM12/8/96
to

holle...@mailbag.com wrote:

> I keep getting a NullPointerException when I try to load
> an image in my application.

[snip]


>
> import java.awt.*;
> import java.applet.*;
>
> public class ImageApplet extends Applet
> {
> Image logo;
> private MediaTracker tracker;
>

> public void init()
> {
> tracker = new MediaTracker (this);
> logo = getImage (getDocumentBase (), "logo.gif");
>
> try
> {
> tracker.waitForAll ();
> }

> catch (InterruptedException e)
> {
> return;
> }
> setBackground (Color.black);
> }
>
> public void paint(Graphics g)
> {
> int width = logo.getWidth(this);
> int height = logo.getHeight(this);
>
> g.drawRect(52, 52, width+10, height + 10);
> g.drawImage(logo, 57, 57, width, height, this);
> }
>
> public static void main(String args[])
> {
> ImageApplet app = new ImageApplet();
> Frame frame = new Frame("Hello world!");
>
> app.init();
> app.start();
>
> frame.add("Center", app);
> frame.resize(400,400);
> frame.show();
> }
> }

The NullPointerException is caused by a reference to the AppletStub, which
hasn't been set. The AppletStub is normally setup by the browser or
AppletViewer, along with placing the Applet in a Frame. If your
instantiate your own Applets, you must do both of these steps.

Doug Bell
db...@shvn.com

Shawn Bertrand

unread,
Dec 19, 1996, 3:00:00 AM12/19/96
to

Daniel Hollenbeck wrote:
>
> Hey All,

>
> I keep getting a NullPointerException when I try to load
> an image in my application.
>
> C:\classes>java ImageApplet
> java.lang.NullPointerException
> at java.applet.Applet.getDocumentBase(Applet.java:59)
> at ImageApplet.init(ImageApplet.java:14)
> at ImageApplet.main(ImageApplet.java:44)
>
> My simple test code is below.
>
> Any ideas what I am doing wrong?
>
> Thanks in advance.
>
> Dan
> holle...@mailbag.com
>

Daniel,

I wasn't aware that an applet written as an application (i.e. with a
main() function) needed to have its init() and start() functions
called. I would try taking those calls out to see what happens.

I have received a NullPointerException on a call to getDocumentBase (or
getCodeBase) before, but it was happening because I was calling it in
the constructor of the applet: when the document base and code base had
not yet been defined.

Therefore, I can only assume that by calling init() before the applet is
shown will cause the NullPointerException to occur because the
underlying applet stub code did not have a chance to initialize the
document base.

Good luck,

Shawn Bertrand
Software Engineer, Oracle Corporation
E-mail: sber...@us.oracle.com

Disclaimer: The statements and opinions expressed here are my own and
do not necessarily represent those of Oracle Corporation.

0 new messages