Results 1 to 5 of 5
- 06-06-2009, 09:46 PM #1
[newbie] Exception in thread "AWT-EventQueue-0" java.lang.Error
Any ideas?
:confused:
Error: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. */
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)
- 06-06-2009, 10:46 PM #2
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 errorJava Code:if ((debug) && (getCodeBase() == null))...
- 06-06-2009, 11:49 PM #3
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)
- 06-07-2009, 12:39 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
> 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:
You can't call toString() on the null returned by applet2.getURL() without throwing an NPE.Java Code:JLabel label2 = new JLabel(applet2.getURL().toString(), SwingConstants.SOUTH);
If a null return value is OK, then you can change the label to cope with that:
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.Java Code:JLabel label2 = new JLabel(String.valueOf(applet2.getURL()), SwingConstants.SOUTH);
- 06-07-2009, 12:59 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
I note that you are getting the NPE thrown still from within the getURL() method.
The following SSCCE will throw an NPE:
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.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()); } }
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
-
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException:
By satishkumar_lskin in forum AWT / SwingReplies: 2Last Post: 12-14-2009, 01:46 AM -
[newbie] Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException
By jon80 in forum New To JavaReplies: 12Last Post: 05-26-2009, 01:48 PM -
[SOLVED] pls help :S . "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerE
By ara in forum New To JavaReplies: 10Last Post: 01-29-2009, 08:00 AM -
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
By iuna in forum AWT / SwingReplies: 12Last Post: 10-05-2008, 06:52 AM -
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException
By leonard in forum New To JavaReplies: 1Last Post: 08-06-2007, 06:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks