Results 1 to 2 of 2
  1. #1
    flamewolf393 is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Default trouble with actionlistener and null pointer exception.

    I have to design a JFrame with a label and 3 buttons, each of the buttons changes the color of the label. The window comes up, and the buttons are displayed fine, but when I click any of the buttons I get this:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ColorSelector$Green_Click.actionPerformed(ColorSel ector.java:75)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.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$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(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)

    Here is my code:

    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;

    class ColorSelector extends JFrame
    {
    public JLabel ColorField;
    public JPanel panel1;
    public JPanel panel2;

    public ColorSelector()
    { super();
    }

    public void start() {
    // Create the panel that holds the labels,
    // text boxes, and buttons.
    createPanel();
    this.setTitle("Color Selector");
    this.setSize(500,500);
    this.setVisible(true);
    }

    public void createPanel() {

    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    // Create the color field
    JLabel ColorField = new JLabel("");
    panel1.add(ColorField);

    // Create the color Buttons
    JButton RedButton = new JButton("Red");
    Red_Click red_click = new Red_Click();
    RedButton.addActionListener(red_click);

    JButton BlueButton = new JButton("Blue");
    Blue_Click blue_click = new Blue_Click();
    BlueButton.addActionListener(blue_click);

    JButton GreenButton = new JButton("Green");
    Green_Click green_click = new Green_Click();
    GreenButton.addActionListener(green_click);

    panel2.add(RedButton);
    panel2.add(BlueButton);
    panel2.add(GreenButton);

    this.add(panel1);
    this.add(panel2);
    }

    //makes click events
    private class Blue_Click implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent event) {
    ColorField.setBackground(Color.blue);
    }
    }
    private class Red_Click implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent event) {
    ColorField.setBackground(Color.red);
    }
    }
    private class Green_Click implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent event) {
    ColorField.setBackground(Color.green);
    }
    }
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: trouble with actionlistener and null pointer exception.

    What is null on that line? When do you ever instantiate the instance variable ColorField?

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 03:22 AM
  3. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 01:12 AM
  4. Help with null pointer exception
    By gammaman in forum New To Java
    Replies: 4
    Last Post: 07-14-2009, 12:23 AM
  5. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 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
  •