简体   繁体   中英

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

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)

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 .

  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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM