0

As stated above this code doesnt work when i run it (sorry for adding so many debugging comments)

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
}

}

and this is these are the line which give the error,

    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");

with this error

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)

What i am trying to do is to run a applet named StartingClass by running its init() function from the main method of another class (this is just for testing, I will finally add this code to a jButton in a fancy jFrame). I if you have any work around without replacing the Applet (cause this is for a school project) i would gladly accept it. Right now, I'm stuck wiht the getImage function which returns a NullPointer Exception. Thanks for helping in advance.

Edit 1

This is different from this question over here as I am not interested in primarily solving a NullPointer Exception of a Image, but to improve my code by eliminating the errors. Also the link didn't solve my problem.

Community
  • 1
  • 1
Roofi
  • 82
  • 1
  • 3
  • 15
  • 1
    Why don't you tell us which line of code the nullpointerexception happens at? – satnam Nov 23 '16 at 17:59
  • 2
    Welcome to Stack Overflow. There's a *lot* of code here, most of which looks irrelevant. Please reduce this to a [mcve]. – Jon Skeet Nov 23 '16 at 17:59
  • 1
    You seem to be writing a main, standalone app. Not an applet. So you shouldn't use Applet and AppletContext. If you're writing an applet, then just don't. They're dead and buried. – JB Nizet Nov 23 '16 at 18:01
  • Yes @Nizet, I completely understand your point but right now it's necessary for to submit this at least as an Applet instead of starting from scratch. – Roofi Nov 23 '16 at 18:03
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – ΦXocę 웃 Пepeúpa ツ Nov 23 '16 at 18:04
  • @ΦXocę웃Пepeúpaツ, yes maybe but I actually want to run this applet from a jButton by simply clicking. – Roofi Nov 23 '16 at 18:07
  • 1
    An applet is supposed to be embedded in a web page, or run by the applet viewer. Not just run as a main application. Why are you using Applet and AppletContext? – JB Nizet Nov 23 '16 at 18:15
  • [This](http://www.kilobolt.com/game-development-tutorial.html) tutorial is the reason i tried using an Applet. – Roofi Nov 23 '16 at 18:21
  • Find a more modern tutorial. See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). – Andrew Thompson Nov 26 '16 at 01:25

0 Answers0