Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: NullPointerException

  1. #1
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default NullPointerException

    hi all,

    I'm getting the following due to the setText call in the change event handler. I must be missing something obvious but I don't see it right now!

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javaapplication4.Main.stateChanged(Main.java:49)
    at javax.swing.AbstractButton.fireStateChanged(Abstra ctButton.java:1889)
    at javax.swing.AbstractButton$Handler.stateChanged(Ab stractButton.java:2310)
    at javax.swing.DefaultButtonModel.fireStateChanged(De faultButtonModel.java:333)
    at javax.swing.DefaultButtonModel.setMnemonic(Default ButtonModel.java:274)
    at javax.swing.AbstractButton.setMnemonic(AbstractBut ton.java:1548)
    at javax.swing.AbstractButton.setMnemonic(AbstractBut ton.java:1569)
    at javaapplication4.Main.<init>(Main.java:29)
    at javaapplication4.Main$1.run(Main.java:42)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

    public class Main implements ChangeListener {
     
        JButton jbtnFirst;
        JLabel jlab;
     
        Main() {
            JFrame jfrm = new JFrame("Testing Buttons!");
            jfrm.setLayout(new FlowLayout());
            jfrm.setSize(350, 350);
            jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            jbtnFirst = new JButton("First");
            jbtnFirst.addChangeListener(this);
            jbtnFirst.setMnemonic('F');
            jfrm.add(jbtnFirst);
     
            jlab = new JLabel();
            jfrm.add(jlab);
     
            jfrm.setVisible(true);
        }
     
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
     
                public void run() {
                    new Main();
                }
            });
     
        }
     
        public void stateChanged(ChangeEvent ce) {
            jlab.setText("aa"); //What is wrong with this?
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: NullPointerException

    Your change listener is fired before you create the jlab variable...it is fired when the component is changed in some way (eg setMnemonic). see code below:
    public class Main implements ChangeListener {
     
        JButton jbtnFirst;
        JLabel jlab;
     
        Main() {
            JFrame jfrm = new JFrame("Testing Buttons!");
            jfrm.setLayout(new FlowLayout());
            jfrm.setSize(350, 350);
            jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            jbtnFirst = new JButton("First");
            jbtnFirst.addChangeListener(this);
            jbtnFirst.setMnemonic('F');
            jfrm.add(jbtnFirst);
            System.out.println("Creating Label");
            jlab = new JLabel();
            jfrm.add(jlab);
     
            jfrm.setVisible(true);
        }
     
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
     
                public void run() {
                    new Main();
                }
            });
     
        }
     
        public void stateChanged(ChangeEvent ce) {
          System.out.println("Changed");//  jlab.setText("aa"); //What is wrong with this?
        }
    }
    Any reason you are not using an ActionListener as opposed to a ChangeListener?
    Last edited by copeg; July 29th, 2010 at 06:40 PM.

  3. The Following User Says Thank You to copeg For This Useful Post:

    bbr201 (July 29th, 2010)

  4. #3
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default Re: NullPointerException

    Ahh, I see. I didn't know using setMnemonic qualified as a change event. Thanks!

  5. #4
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default Re: NullPointerException

    Well, I just got to the ChangeListener section in the book. So I was just trying it out to see how it worked.

Similar Threads

  1. Funny NullPointerException
    By Marcus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 6th, 2009, 10:14 AM
  2. [SOLVED] NullPointerException.CLARIFICATION
    By chronoz13 in forum Exceptions
    Replies: 8
    Last Post: August 28th, 2009, 03:24 PM
  3. [SOLVED] NullPointerException in Poker's program
    By dean_martin in forum Exceptions
    Replies: 10
    Last Post: April 21st, 2009, 06:40 AM
  4. Replies: 3
    Last Post: February 26th, 2009, 03:04 AM
  5. NullPointerException in Java
    By jazz2k8 in forum Exceptions
    Replies: 9
    Last Post: June 1st, 2008, 05:59 PM