JApplet in appletviewer / JRE1.6.0_30 — NullPointerException on getParameter("someArg")

742 views Asked by At

Why am I getting a NullPointerException when I call getParameter() in this very simple JApplet instantiation?

public class TestPad extends javax.swing.JApplet {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestPad appletDefn = new TestPad();

                TestPad.sSomeParam = (String)appletDefn.getParameter("sSomeParam");

                appletDefn.init();

                appletDefn.start();
            }
        });
    }

    private static String sSomeParam = "sSomeArg";

}

No security policy file, no other packages, and only two libraries: a) swing-layout-1.0.4.jar b) JDK-1.6 (default)

2

There are 2 answers

0
xehpuk On

The implementation of the method in the Applet class:

 public String getParameter(String name) {
     return stub.getParameter(name);
 }

So the method call on transient private AppletStub stub throws the exception.
Applets have an other lifecycle than a normal application. I suggest you to take a look at the official Java tutorials on Applets.

0
Andrew Thompson On
  1. That code throws no NPE when run in the applet viewer here. This is no surprise to me, since it would load the public applet class, then invoke init() and run(). At no time would it call the main(String[]).
  2. Which leads me to the conclusion that you are running the 'applet' by calling the main(String[]), not using the applet viewer. Running it that way will cause an NPE because there has been no applet context/stub set up and initialized. It takes some work to do so.