tags:

views:

544

answers:

2

I'm trying to make a simple class to open a URL but when I run it it gives a null pointer exception as soon as I initialize the Applet. Here is the class:

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());
        }
    }
}

And I am calling it like this:

URLOpener opener = new URLOpener();
String URL = "http://somewebsite";
opener.openURL(URL);

It gives the exception at the line AppletContext applet = getAppletContext();

Can anyone explain what I am doing wrong? Thanks.

Edit: Here is the full stacktrace.

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)

+1  A: 

Since you don't post the stacktrace I'll guess:

You're using this class as a standalone java application and thus, there is no applet context defined.

Did I guess right?

OscarRyz
A: 

Use the Desktop class.

Desktop.getDesktop().browse(new URL("http://google.com").toURI());

You can't use applets outside of a browser.

Milan Ramaiya
I had tried that originally but got an "Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: java/awt/Desktop" error and a warning saying that java.awt.Desktop is never used.
Mike2012