简体   繁体   English

appletviewer / JRE1.6.0_30中的JApplet - getParameter上的NullPointerException(“someArg”)

[英]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? 当我在这个非常简单的JApplet实例化中调用getParameter()时,为什么会出现NullPointerException

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) 没有安全策略文件,没有其他包,只有两个库:a)swing-layout-1.0.4.jar b)JDK-1.6(默认)

The implementation of the method in the Applet class: Applet类中方法的实现:

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

So the method call on transient private AppletStub stub throws the exception. 因此,对transient private AppletStub stub的方法调用会抛出异常。
Applets have an other lifecycle than a normal application. Applet具有除正常应用程序之外的其他生命周期。 I suggest you to take a look at the official Java tutorials on Applets . 我建议你看一下关于Applets官方Java教程

  1. That code throws no NPE when run in the applet viewer here. 在这里的applet查看器中运行时,该代码不会抛出NPE。 This is no surprise to me, since it would load the public applet class, then invoke init() and run() . 这对我来说并不奇怪,因为它会加载公共applet类,然后调用init()run() At no time would it call the main(String[]) . 它在任何时候都不会调用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. 这让我得出的结论是,您通过调用main(String[])来运行'applet',而不是使用applet viewer。 Running it that way will cause an NPE because there has been no applet context/stub set up and initialized. 以这种方式运行导致NPE,因为没有设置和初始化applet上下文/存根。 It takes some work to do so. 这需要一些工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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