我正在尝试使用jnlp运行JApplet。
我创建了MyApplet,它扩展了JApplet,并包装在一个罐子中。
我还创建了MyApplet.jnlp和MyApplet.html
我的运行时环境是jdk 1.7.0.02。

当我在浏览器中运行它时,我从浏览器中得到异常提示,但是我的applet作为一个独立的设备从Eclipse中正常运行

这是我得到的例外

Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException
            at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter$8.run(Unknown Source)
            at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.runOnEDT(Unknown Source)
            at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.resize(Unknown Source)


请在下面找到我的代码。这是在Eclipse中运行良好的applet类:

        import java.awt.Color;
        import java.awt.Container;
        import java.awt.Graphics;

        import javax.swing.JApplet;
        //This is my applet class
        public class MyApplet extends JApplet {

            private Container Panel;
        //constructor
              public MyApplet () {
                super ();
                Panel = getContentPane();
                Panel.setBackground (Color.cyan);
              }

    //paint method
              public void paint (Graphics g) {
                int Width;
                int Height;

                super.paint (g);
                Width = getWidth();
                Height = getHeight();
                g.drawString ("The applet width is " + Width + " Pixels", 10, 30);
                g.drawString ("The applet height is " + Height + " Pixels", 10, 50);
              }

        }


这是html文件MyApplet.html:

                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
                <html lang="en-US">
                  <head>
                    <title>My Applet Menu Chooser Applet</title>
                    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
                  </head>
                  <body>
                    <noscript>A browser with JavaScript enabled is required for this page to operate properly.</noscript>
                    <h1>Draggable Menu ChooserApplet</h1>
                    <p>Press the <b>Left</b> button on your mouse and drag the applet.</p>


                    <script src="http://www.java.com/js/deployJava.js"></script>
                    <script>
                        var attributes = { code:'MyApplet', width:900, height:300 };
                        var parameters = {jnlp_href: 'MyApplet.jnlp', draggable: 'true'} ;
                        deployJava.runApplet(attributes, parameters, '1.6');
                    </script>
                  </body>
                </html>


MyAppletJnlp.jnlp

                <?xml version="1.0" encoding="utf-8"?>
                <jnlp spec="1.0+"  href="MyApplet.jnlp">
                    <information>
                        <title>Jnlp Testing</title>
                        <vendor>akash sharma</vendor>
                        <description>Testing Testing</description>
                         <offline-allowed />
                        <shortcut online="false">
                            <desktop />
                        </shortcut>
                    </information>

                    <security>
                        <all-permissions/>
                    </security>
                    <resources>
                        <!-- Application Resources -->

                              href="http://java.sun.com/products/autodl/j2se"/>
                        <jar href="MyApplet.jar" main="true" />
                    </resources>
                    <applet-desc
                         name="Draggable Applet"
                         main-class="com.acc.MyApplet"
                         width="900"
                         height="300">
                     </applet-desc>
                     <update check="background"/>

                </jnlp>

最佳答案

我找到了解决方案。实际上在jar文件中,类文件的路径与我在JNLP文件中提到的路径不正确,这就是为什么我得到空指针异常的原因。一旦我更新了jar文件,我就解决了

关于java - 线程AWT-EventQueue-2中的异常java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7565185/

10-12 02:30