Results 1 to 5 of 5
Thread: What's wrong with this Applet?
- 09-15-2011, 11:59 PM #1
Member
- Join Date
- Sep 2011
- Location
- Estonia
- Posts
- 1
- Rep Power
- 0
What's wrong with this Applet?
Hi,
I wrote the following Applet. When I run it in a browser, it shows the "Start" button,
but when I click on it, I get a java.lang.NullPointerException.
Even the "Hello?" doesn't get displayed. What am I doing wrong?
Thanks for any hint!
Luc
Here's the Applet code:
Java Code:import java.awt.*; import java.applet.Applet; import java.awt.event.*; import java.awt.Desktop; import java.io.File; import java.io.IOException; public class StartFile extends Applet implements ActionListener { public Desktop desktop; public Button btn; public void init() { btn = new Button("Start"); add(btn); btn.addActionListener(this); } public void actionPerformed(ActionEvent event) { System.out.println("Hello?"); //~ if (Desktop.isDesktopSupported()) {} if (event.getSource() == btn) { String fileName = "c:\\temp\\foo.txt"; // txtFile.getText(); System.out.println(fileName); File file = new File(fileName); try { desktop = Desktop.getDesktop(); desktop.open(file); //~ desktop.edit(file); //~ desktop.print(file); } catch (IOException ioe) { ioe.printStackTrace(); } } } }
Here's the Java Console output:
Java Code:Exception in thread "AWT-EventQueue-11" java.lang.NullPointerException at StartFile.actionPerformed(StartFile.java:27) at java.awt.Button.processActionEvent(Unknown Source) at java.awt.Button.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(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)
-
Re: What's wrong with this Applet?
Which line is line 27 in your StartFile code as noted by: "StartFile.java:27"?
And how do you know that "Hello" isn't displayed? Have you inspected a Java console?
- 09-16-2011, 01:44 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: What's wrong with this Applet?
So, which is line 27?
------
In general the NullPointerException occurs when you dereference (use) variable as if it had a non null value when, in fact, it was null.
Once you have identified the null variable (or expression) you have to look at where you assigned it a value that you thought - or hoped!- was non null. Then figure out why that didn't happen. The javadocs are your friend here as some methods do return null.Java Code:arr[bar] = baz; // but arr is null foo.meth(arg); // but foo is null
- 09-16-2011, 02:10 AM #4
Re: What's wrong with this Applet?
You have a println first in the actionPerformed method that should have printed in the Java console.
What isn't that printout shown in your post of the java console contents?
- 09-16-2011, 05:55 AM #5
Member
- Join Date
- Sep 2011
- Location
- Estonia
- Posts
- 1
- Rep Power
- 0
Re: What's wrong with this Applet?
Oops, the problem wasn't in the code at all!
It seems that I must restart my browser each time I recompiled a Java applet. I didn't notice this yesterday evening. This morning the applet worked as expected, displaying "Hello?" and "c:\temp\foo.txt" in the Java Console.
I guess I should rather use applet viewer when testing applets.
Thanks for your replies which helped me indirectly to find the explanation.
Luc
Similar Threads
-
Java Applet starts then the applet field goes light blue
By oki in forum Java AppletsReplies: 30Last Post: 08-26-2011, 09:05 PM -
Beginner. What's wrong in my applet declaration?
By rforte in forum New To JavaReplies: 4Last Post: 04-16-2010, 11:01 AM -
What's wrong with this applet?
By Arnold in forum New To JavaReplies: 9Last Post: 11-22-2009, 05:21 PM -
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 2Last Post: 11-22-2009, 03:48 PM -
Applet, To center text and To open I engage in a dialog in an Applet
By Marcus in forum Java AppletsReplies: 4Last Post: 06-08-2007, 06:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks