我正在用 Java 制作一个基于小程序的游戏,但我遇到了问题。我收到带有以下堆栈跟踪的空指针异常:
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)
ImageEntity.java 第 84 行的代码如下:
image = applet.getImage( getURL( filename ) );
使用测试打印,applet
不为空,也不为getURL( filename )
.
打印getUrl( filename )
退货file:/home/glenn/Documents/code/RMGame/RMPatrolGame/house7.gif
。
为什么我得到一个空指针异常?