IE8 / JRE1.6.0_24中的JApplet - getAppletContext()&上的NullPointerException JSObject.getWindow()

时间:2011-04-07 22:59:08

标签: java html applet nullpointerexception

我有一个JApplet,我在IE8中加载了JRE Plugin v 1.6.0_24,每当我尝试使用回调到网页时(ala getAppletContext()或者JSObject.getWindow())我得到了一个例外。基本上,似乎IE不会将我的JApplet视为实际的applet,因为标签可能?或者我在JApplet中做错了。

这是我用来嵌入JApplet的HTML Object标签:

 <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;

}

this.getAppletContext()上的NullPointerException。showDocument(url,“_ self”)

1 个答案:

答案 0 :(得分:3)

我会从症状中做一些猜测。

您在Applet.getParameterApplet.getAppletContext()中有NullPointerExceptions。这两种方法都获取AppletStub并调用此对象的方法。所以我的猜测是你的AppletStub是null

为什么它可以为空?如果您的Applet不是浏览器中的顶级Applet,但是某些组件放在另一个Applet中,则它可以为null。看看这个。