URLを開くための簡単なクラスを作成しようとしていますが、実行するとアプレットを初期化するとすぐにnullポインタの例外が発生します。ここではクラスがある:URLを新しいウィンドウで開くJava
package com.agentsheets.component;
import java.net.*;
import java.applet.*;
public class URLOpener extends Applet{
public void openURL(String inputURL) {
try {
AppletContext applet = getAppletContext();
URL url = new URL(inputURL);
applet.showDocument(url, "Ristretto");
}
catch (MalformedURLException e){
System.out.println(e.getMessage());
}
}
}
そして、私はこのようにそれを呼び出しています:
URLOpener opener = new URLOpener();
String URL = "http://somewebsite";
opener.openURL(URL);
それはラインAppletContext applet = getAppletContext();
で例外を与え、誰もが私が間違っているのかを説明することはできますか?ありがとう。
編集:ここに完全なスタックトレースがあります。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at java.applet.Applet.getAppletContext(Unknown Source) at com.agentsheets.component.URLOpener.openURL(URLOpener.java:21) at com.agentsheets.component.ComponentController.actionPerformed(ComponentController.java:2510) at javax.swing.JComboBox.fireActionEvent(Unknown Source) at javax.swing.JComboBox.setSelectedItem(Unknown Source) at javax.swing.JComboBox.setSelectedIndex(Unknown Source) at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source) at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
正確な例外スタックトレースを投稿できますか? – akarnokd