Results 1 to 5 of 5
  1. #1
    lsaffre is offline Member
    Join Date
    Sep 2011
    Location
    Estonia
    Posts
    1
    Rep Power
    0

    Default 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)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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?

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

    Default 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.

    Java Code:
    arr[bar] = baz; // but arr is null
    foo.meth(arg); // but foo is 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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default 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?

  5. #5
    lsaffre is offline Member
    Join Date
    Sep 2011
    Location
    Estonia
    Posts
    1
    Rep Power
    0

    Default 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

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. Replies: 30
    Last Post: 08-26-2011, 09:05 PM
  3. Beginner. What's wrong in my applet declaration?
    By rforte in forum New To Java
    Replies: 4
    Last Post: 04-16-2010, 11:01 AM
  4. What's wrong with this applet?
    By Arnold in forum New To Java
    Replies: 9
    Last Post: 11-22-2009, 05:21 PM
  5. Replies: 4
    Last Post: 06-08-2007, 06:15 AM

Posting Permissions

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