I'm making an applet based game in Java, but I'm running into a problem. I'm getting a null pointer exception with the following stack trace:
at java.applet.Applet.getAppletContext(Applet.java:204)
at java.applet.Applet.getImage(Applet.java:274)
at ImageEntity.load(ImageEntity.java:84) <==lowest code written by me
at SpriteTest.<init>(SpriteTest.java:14)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:374)
at java.lang.Class.newInstance(Class.java:327)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:795)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:724)
at sun.applet.AppletPanel.run(AppletPanel.java:380)
at java.lang.Thread.run(Thread.java:722)
The code at ImageEntity.java line 84 is as follows:
image = applet.getImage( getURL( filename ) );
Using a test print, applet
isn't null, nor is getURL( filename )
.
Printing getUrl( filename )
returns file:/home/glenn/Documents/code/RMGame/RMPatrolGame/house7.gif
.
Why am I getting a null pointer exception?
Here is the code for java.applet.Applet.getAppletContext(Applet.java:204):
public AppletContext getAppletContext() {
return stub.getAppletContext();
}
'stub' is null. AppletPanel.runLoader()
calls setStub() .
Your code is calling getImage
before the Applet has had a chance to initialize (ie stub is still null).
Call getImage()
in the init() method. Do not call it in a constructor or during field initialization.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.