I am new in Java programming, i want to write a java applet
which calls a constructor to draw a image. But the appletviewer
show me an exception, what's wrong I have done?
How can I modify the code?
The error message:
Exception occurred during event dispatching:
java.lang.NullPointerException
at sun.awt.windows.WGraphics.drawImage(WGraphics.java:272)
at TestApplet.paint(TestApplet.java:38)
at sun.awt.windows.WComponentPeer.handleEvent(Compiled Code)
at java.awt.Component.dispatchEventImpl(Compiled Code)
at java.awt.Container.dispatchEventImpl(Compiled Code)
at java.awt.EventDispatchThread.run(Compiled Code)
The source:
import java.applet.*;
import java.awt.*;
class TestClass
{
TestApplet applet;
Image img1;
public TestClass(TestApplet applet)
{
this.applet=applet;
this.applet.getImage(this.applet.getCodeBase(),"test.gif");
}
public void showimg()
{
applet.x=0;
applet.y=0;
applet.currimg=img1;
applet.repaint();
}
}
public class TestApplet extends Applet
{
TestClass tc;
public Image currimg;
public int x,y;
public void init()
{
tc=new TestClass(TestApplet.this);
tc.showimg();
}
public void paint(Graphics g)
{
g.drawImage(currimg,x,y,this);
}
}
On Tue, 27 Oct 1998 21:46:42 +0800, "Lawrence" <uzuk...@hotmail.com<please_remove_this_extension>>
wrote:
| Hello all,
|
| I am new in Java programming, i want to write a java applet
| which calls a constructor to draw a image. But the appletviewer
| show me an exception, what's wrong I have done?
| How can I modify the code?
|
| The error message:
|
| Exception occurred during event dispatching:
| java.lang.NullPointerException
| at sun.awt.windows.WGraphics.drawImage(WGraphics.java:272)
| at TestApplet.paint(TestApplet.java:38)
| at sun.awt.windows.WComponentPeer.handleEvent(Compiled Code)
| at java.awt.Component.dispatchEventImpl(Compiled Code)
| at java.awt.Container.dispatchEventImpl(Compiled Code)
| at java.awt.EventDispatchThread.run(Compiled Code)
|
| The source:
|
| import java.applet.*;
| import java.awt.*;
|
| class TestClass
| {
| TestApplet applet;
| Image img1;
| public TestClass(TestApplet applet)
| {
| this.applet=applet;
| this.applet.getImage(this.applet.getCodeBase(),"test.gif");
The getImage() methods return immediately, so that you don't have to wait for an image to be loaded
before going on to perform other operations in your program. While this improves performance, some
programs require more control or information about image loading. You can track image loading status
either by using the MediaTracker class or by implementing the imageUpdate() method, which is defined
by the ImageObserver interface.
For a detailed explanation:
See <http://java.sun.com/docs/books/tutorial/ui/drawing/usingImages.html>,
and, in particular <http://java.sun.com/docs/books/tutorial/ui/drawing/loadingImages.html>
davep
--
Dave Postill Investment Intelligence Systems Corp
Galaxy Support Leader +44 (0)171 628 6960 [voice]
da...@iisc.co.uk [business] +44 (0)171 638 7528 [fax]
dave.p...@pobox.com [personal] http://www.iisc.co.uk
davep...@my-dejanews.com [personal]
The simpler alternative to your code would be
import java.applet.*;
import java.awt.*;
public class test extends Applet
{
Image myImage;
public void init()
{
myImage = getImage(getCodeBase(),"test.gif");
}
public void paint(Graphics g)
{
g.drawImage(myImage,0,0,this);
}
}
Lawrence