我有一个用IE8中的JRE插件v 1.6.0庠24加载的JApplet,每当我试图使用网页回调(ala getAppletContext()或JSObject.getWindow())时,都会收到异常。基本上看起来IE并没有因为标签而把我的JApplet当作一个真正的applet?要么是那样,要么是我的日语做错了什么。
下面是我用来嵌入JApplet的HTML对象标记:

 <object codetype="application/java"
     classid="clsid:CAFEEFAC-0016-0000-0024-ABCDEFFEDCBA"
     codebase="http://java.sun.com/update/1.6.0/jinstall-6u24-windows-i586.cab"
     width="640"
     height="480"
     id="ImageEdit">

   <param name="code" value="my.imaging.test.class"/>
   <param name="codebase" value="/folder/" />
   <param name="type" value="application/x-java-applet;jpi-version=1.6.0_24" />
   <param name="cache_archive" value="test.jar, jai_core.jar, jai_codec.jar,
                                       mlibwrapper_jai.jar, axis.jar, jaxrpc.jar,
                                       jbig2.jar, PDFRenderer.jar"/>
   <param name="cache_version"  value="1.0.2.F, 1.1.3, 1.1.3, 1.1.3,
                                        1.0, 1.0, 1.0, 1.0"/>
   <param name="cache_archive_ex" value="test.jar;preload;1.0.2.F"/>
   <param name="cache_option" value="Plugin"/>
   <param name="scriptable" value="true"/>
   <param name="mayscript" value="true"/>
 </object>

以下是其中一个JSException的相关部分:
Exception in thread "AWT-EventQueue-4" netscape.javascript.JSException
      at netscape.javascript.JSObject.getWindow(Unknown Source)
      at my.imaging.test.lblLoadPageMouseReleased(test.java:2180)
      at my.imaging.test.access$6600(test.java:74)
      at my.imaging.test$15.mouseReleased(test.java:1621)
      at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)...

Caused by: java.lang.NullPointerException
     at java.applet.Applet.getParameter(Unknown Source)
     ... 35 more

以及getAppletContext()中的NullPointerException:
java.lang.NullPointerException
      at java.applet.Applet.getAppletContext(Unknown Source)
      at my.imaging.test.lblCloseMouseReleased(test.java:2019)
      at my.imaging.test.access$6600(test.java:74)
      at my.imaging.test$15.mouseReleased(test.java:1621)
      at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)

所以在我的applet中,显然我在扩展javax.swing.JApplet并重写init()。我的回调发生在一个鼠标释放事件上(如异常所示)。
private void lblLoadPageMouseReleased(java.awt.event.MouseEvent evt) {
      JSObject window = null;
      URL url = null;
      try {
        url = new URL("http://127.0.0.1/folder/newpage.html");
        if (imageList.size() > 0) {
            if (JOptionPane.showConfirmDialog(rootPane, "Are you sure?",
               "Question", JOptionPane.YES_NO_OPTION,
               JOptionPane.QUESTION_MESSAGE, infoIcon) == JOptionPane.OK_OPTION)
            {
               this.getAppletContext().showDocument(url, "_self");
            }
        } else {
            window = JSObject.getWindow(this);
            window.eval("nextPage()");
        }
    } catch(JSException jse){
        jse.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我被这件事难住了。另外,仅供参考——我不想从命令行运行它,plugin.jar在我的类路径上。任何关于我做错了什么的想法都会非常感激。。。谢谢!
编辑:下面的SSCCE
public class NewJApplet extends javax.swing.JApplet {

/** Initializes the applet NewJApplet */
@Override
public void init() {
    try {
        java.awt.EventQueue.invokeAndWait(new Runnable() {
            public void run() {
                initComponents();
            }
        });

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

@SuppressWarnings("unchecked")
private void initComponents() {
    btnDo = new javax.swing.JButton();
    btnDo.setText("Do Something");
    btnDo.addActionListener(new java.awt.event.ActionListener() {

        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnDoActionPerformed(evt);
        }
    });
    this.getContentPane().add(btnDo);
}

private void btnDoActionPerformed(java.awt.event.ActionEvent evt) {
    URL url = null;
    try {
        url = new URL("http://127.0.0.1/folder/newpage.html");
        this.getAppletContext().showDocument(url, "_self");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
private javax.swing.JButton btnDo;

}
此.getAppletContext().showDocument上的NullPointerException(url,“\u self”)

最佳答案

我会根据症状做些猜测。
您在Applet.getParameterApplet.getAppletContext()中有NullPointerExceptions。这两个方法都获取AppletStub并调用此对象的方法。所以我猜你的AppletStub是null
为什么它可以为空?如果小程序不是浏览器中的顶级小程序,而是放在另一个小程序中的某个组件,则可以为空。看看这个。

关于java - IE8/JRE1.6.0_24中的JApplet — getAppletContext()和JSObject.getWindow()上的NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5588607/

10-10 22:31