Results 1 to 3 of 3

Thread: Event Handling

  1. #1
    hitesh1907 is offline Member
    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Default Event Handling

    Hi ,

    I was practicing Event Handling and is getting the error in program. Can you please spare some time in letting me know as what i am doing wrong.

    import java.awt.*;
    import java.awt.event.*;

    class A implements ActionListener{
    TextField tt;
    A()
    {

    Frame f = new Frame();
    Button b = new Button("cl");
    b.addActionListener(this);
    TextField tt = new TextField();
    tt.setBounds(60,50,170,20);
    f.add(b);
    f.add(tt);
    b.setBounds(110,120,80,30);
    f.setSize(300,300);
    f.setLayout(null);
    f.setVisible(true);
    }

    public void actionPerformed(ActionEvent ae)
    {
    String s = ae.getActionCommand();
    if (s.equals("cl"))
    {
    tt.setText("Wel");
    }
    }
    public static void main (String args[])
    {
    A a = new A();

    }
    }


    Errror:

    Error: When i click button cl
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at A.actionPerformed(A.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$200(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.doIntersectionPri vilege(Unknown Sou
    ce)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sou
    ce)
    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.doIntersectionPri vilege(Unknown Sou
    ce)
    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
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Event Handling

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at A.actionPerformed(A.java:27)
    Something is null on line 27. Add some println's in there to figure out what variable is null, then backtrack to where you think the variable might be initialized to determine why it might be null

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: Event Handling

    You're redeclaring tt as a TextField which is shadowing the instance variable if the same name. So the instance variable remains null. BTW, any particular reason you are not using Swing components? Button, Frame, TextField as opposed to JButton, JFrame, JTextField?

    Regards,
    Jim
    Last edited by jim829; 06-03-2013 at 01:58 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. JSP Event Handling!!!
    By hassaanid2012 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 06-22-2012, 03:25 PM
  2. Event handling... help?
    By kevzspeare in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 08:46 PM
  3. Event handling in JSF
    By java08 in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 03-24-2009, 06:42 AM
  4. rmi and event handling
    By darkhorse in forum Advanced Java
    Replies: 0
    Last Post: 03-15-2009, 08:20 AM
  5. Event Handling
    By luisarca in forum Sun Java Wireless Toolkit
    Replies: 5
    Last Post: 05-07-2007, 06:05 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
  •