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 .
init()
and run()
. At no time would it call the main(String[])
. 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.