Results 1 to 5 of 5
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [newbie] Exception in thread "AWT-EventQueue-0" java.lang.Error

    Any ideas?

    :confused:

    Java Code:
    [B]AppletFrame.java[/B]
    package homenetwork.bkr.training;
    
    import java.awt.*;
    import java.applet.*;
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class AppletFrame extends JFrame implements AppletStub, AppletContext {
    	
    	public AppletFrame(Applet anApplet)
    	{
    		applet = anApplet;
    		add(applet);
    		applet.setStub(this);
    	}
    	
    	public void setVisible(boolean b)
    	{
    		if (b)
    		{
    			applet.init();
    			super.setVisible(true);
    			applet.start();
    		}
    		else 
    		{
    			applet.stop();
    			super.setVisible(false);
    			applet.destroy();
    		}
    	}
    	
    	//AppletStub methods //overrides?
    	public boolean isActive() {return true;}
    	[B]public URL getDocumentBase() {return null;}
    	public URL getCodeBase() {return null;}[/B]
    //Are these @overrides?
    	public String getParameter(String name) {return "";}
    	public AppletContext getAppletContext() {return this;}
    	public void appletResize (int width, int height) {}
    	
    	//AppletContext methods
    	public AudioClip getAudioClip(URL url) {return null;}
    	public Image getImage (URL url) {return Toolkit.getDefaultToolkit().getImage(url);}
    	public Applet getApplet(String name) {return null;}
    	public Enumeration<Applet> getApplets() {return null;}
    	public void showDocument(URL url) {}
    	public void showDocument(URL url, String target) {}
    	public void showStatus(String status) {}
    	public void setStream(String key, InputStream stream) {}
    	public InputStream getStream (String key) {return null;}
    	public Iterator<String> getStreamKeys() {return null;}
    	
    	private Applet applet;
    	
    	
    	
    	
    }
    
    [B]AppletApplication.java[/B]
    package homenetwork.bkr.training;
    import java.awt.EventQueue;
    import javax.swing.*;
    
    public class AppletApplication extends NotHelloWorldApplet {
    	
    	public static void main (String[] args)
    	{
    		EventQueue.invokeLater(new Runnable()
    		{
    			public void run()
    			{
    				AppletFrame frame = new AppletFrame(new NotHelloWorldApplet());
    				frame.setTitle("Not Hello World Applet");
    				frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.setVisible(true);
    			}
    		});
    	}
    	
    	public static final int DEFAULT_WIDTH = 200;
    	public static final int DEFAULT_HEIGHT = 200;
    
    }
    
    
    [B]NotHelloWorldApplet.java[/B]
    package homenetwork.bkr.training;
    
    import java.applet.Applet;
    import java.awt.*;
    import javax.swing.*;
    import java.net.URL;
    
    /*
     * <applet code="NotHelloWorldApplet.class" width="300" height="300">
     * </applet>
     */
    @SuppressWarnings("serial")
    public class NotHelloWorldApplet extends JApplet {
    	
    	public void init()
    	{
    		EventQueue.invokeLater(new Runnable()
    		{
    			public void run()
    			{
    				JLabel label = new JLabel("Not a Hello World Applet", SwingConstants.CENTER);
    				add(label);
    				
    				anotherApplet applet2 = new anotherApplet("some text");
    				JLabel label2 = new JLabel(applet2.getURL().toString(), SwingConstants.SOUTH);
    				add(label2);
    			}
    		});
    	}
    	
    	@SuppressWarnings("unused")
    	private class anotherApplet extends Applet
    	{
    		public anotherApplet(String textToDisplay)
    		{
    			JLabel label = new JLabel(textToDisplay); 
    		}
    		
    		public URL getURL()
    		{	
    			if ((debug) && (getCodeBase == null)) System.out.println("null");
    			return getCodeBase();
    		}
    		
    		private boolean debug = true;
    	}
    
    }
    /*FIX
     Warning displayed:
     Someone else had a similar problem (unresolved)
     http://forums.sun.com/thread.jspa?threadID=5277025.
    */
    Error:
    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
    getCodeBase cannot be resolved

    at homenetwork.bkr.training.NotHelloWorldApplet$anoth erApplet.getURL(NotHelloWorldApplet.java:41)
    at homenetwork.bkr.training.NotHelloWorldApplet$1.run (NotHelloWorldApplet.java:25)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    C:\jexp\jon>javac nothelloworldapplet.java
    nothelloworldapplet.java:39: cannot find symbol
    symbol  : variable getCodeBase
    location: class NotHelloWorldApplet.anotherApplet
                if ((debug) && (getCodeBase == null)) System.out.println("null");
                                ^
    1 error
    Java Code:
    if ((debug) && (getCodeBase() == null))...

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    That's right, sorry. getCodeBase() still returns a java.lang.NullPointerException.

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.applet.Applet.getCodeBase(Unknown Source)
    at homenetwork.bkr.training.NotHelloWorldApplet$anoth erApplet.getURL(NotHelloWorldApplet.java:41)
    at homenetwork.bkr.training.NotHelloWorldApplet$1.run (NotHelloWorldApplet.java:25)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    > That's right, sorry. getCodeBase() still returns a java.lang.NullPointerException.

    Assuming you have changed the offending line the way hardwired suggested, then it doesn't return an NPE: what it returns is getCodeBase().

    Now if getCodeBase() happens to be null there is going to be a problem back in the run() method where you say:

    Java Code:
    JLabel label2 = new JLabel(applet2.getURL().toString(), SwingConstants.SOUTH);
    You can't call toString() on the null returned by applet2.getURL() without throwing an NPE.

    If a null return value is OK, then you can change the label to cope with that:

    Java Code:
    JLabel label2 = new JLabel(String.valueOf(applet2.getURL()), SwingConstants.SOUTH);
    If a null return value is not OK, then you should do something about that - something more than just printing "null" to the console but then just returning the null value to cause a problem later on.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I note that you are getting the NPE thrown still from within the getURL() method.

    The following SSCCE will throw an NPE:

    Java Code:
    import java.applet.Applet;
    
    public class Test {
        public static void main(String[] args) throws Exception {
            Applet foo = new Applet();
            System.out.println(foo.getCodeBase());
        }
    }
    The problem seems to be that foo is declared as Applet, and constructed with new Applet(). But it neither walks, talks or quacks like an applet.

    In particular applets - honest to goodness ones - have a private member variable of type AppletStub. When you say things like getCodeBase() to the applet, the applet delegates this to its applet stub - that is, it returns whatever the applet stub says is the code base.

    In your case - and in the case of the SSCCE above - this applet stub is null. And hence you get the NPE.

    From the API documentation we read "An applet is a small program that is intended not to be run on its own, but rather to be embedded inside another application." (another application is something like a web browser, or Sun's applet viewer). And "When an applet is first created, an applet stub is attached to it using the applet's <code>setStub</code> method. This stub serves as the interface between the applet and the browser environment or applet viewer environment in which the application is running."

    It seems like your inner class is not doing this setting up of an interface between what advertises itself as an Applet and the browser environment.

    If I were you I would rethink the wisdom of extending Applet as an inner class of a JApplet like this. Or explain what it is that this is trying to do.

Similar Threads

  1. Replies: 2
    Last Post: 12-14-2009, 01:46 AM
  2. Replies: 12
    Last Post: 05-26-2009, 01:48 PM
  3. Replies: 10
    Last Post: 01-29-2009, 08:00 AM
  4. Replies: 12
    Last Post: 10-05-2008, 06:52 AM
  5. Replies: 1
    Last Post: 08-06-2007, 06:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •