使用getImage的NullPointer异常

时间:2016-11-23 17:57:58

标签: java image nullpointerexception applet

如上所述,当我运行它时,这段代码不起作用(抱歉添加了这么多调试注释)

public class TheBigGame extends StartingClass {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
//        StartingClass.main(args);
    StartingClass st = new StartingClass();
    st.init();
//        Frame f = (Frame)(new AppletViewer(st.getWidth(),st.getHeight(), url, hshtbl, stream, avf))
    // TODO code application logic here
}

}

这就是给出错误的行,

    heliboy  = getImage(getClass().getResource("heliboy.png"));
    heliboy2 = getImage(getClass().getResource("heliboy2.png"));
    heliboy3 = getImage(base, "heliboy3.png");
    heliboy4 = getImage(base, "heliboy4.png");
    heliboy5 = getImage(base, "heliboy5.png");

    background = getImage(base, "background.png");

    tiledirt       = getImage(base, "tiledirt.png");
    tilegrassTop   = getImage(base, "tilegrasstop.png");
    tilegrassBot   = getImage(base, "tilegrassbot.png");
    tilegrassLeft  = getImage(base, "tilegrassleft.png");
    tilegrassRight = getImage(base, "tilegrassright.png");

出现此错误

Exception in thread "main" java.lang.NullPointerException
at java.applet.Applet.getAppletContext(Applet.java:204)
at java.applet.Applet.getImage(Applet.java:276)
at StartingClass.init(StartingClass.java:119)
at TheBigGame.main(TheBigGame.java:26)
/home/guest/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java   returned: 1
BUILD FAILED (total time: 0 seconds)

我想要做的是通过从另一个类的main方法运行其init()函数来运行一个名为StartingClass的applet(这只是用于测试,我最终将这个代码添加到一个花哨的jFrame中的jButton )。如果你有任何工作没有更换Applet(因为这是一个学校项目)我很乐意接受它。 现在,我遇到了getImage函数,它返回一个NullPointer异常。感谢您提前帮助。

编辑1

这与this问题不同,因为我对主要解决图像的NullPointer异常不感兴趣,而是通过消除错误来改进我的代码。此链接也没有解决我的问题。

0 个答案:

没有答案
相关问题