1

Hi i'm trying to add actionlistener that would take an input from the user and change the color of the background, but i'm getting this error... would you please be specific and give an answer for a beginner... thanks enter code here

package Quizzes_practice;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class TextField1 extends JFrame {
    private JPanel mainpanel;
    private JLabel text; 
    private JTextField mytext;
    private JTextField bg;


    public TextField1(){
        setLayout(new GridLayout(2,0));
        JPanel mainpanel = new JPanel();
        JLabel text = new JLabel("Enter value: ");
        JTextField mytext = new JTextField(10);
        JTextField bg = new JTextField(40);


        add(mainpanel);
        mainpanel.setLayout(new FlowLayout());
        mainpanel.add(text);
        mainpanel.add(mytext);
        mainpanel.add(bg);
        myhand handler = new myhand();
        mytext.addActionListener(handler);
        bg.addActionListener(handler);


    }
    private class myhand implements ActionListener{
        public void actionPerformed (ActionEvent event){


                int a2 = Integer.parseInt(event.getActionCommand());
                Color mycol= new Color(a2);
                mainpanel.setBackground(mycol);



        }
    }


}

the error i'm getting is

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Quizzes_practice.TextField1$myhand.actionPerformed(TextField1.java:47)
    at javax.swing.JTextField.fireActionPerformed(Unknown Source)
    at javax.swing.JTextField.postActionEvent(Unknown Source)
    at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
    at javax.swing.SwingUtilities.notifyAction(Unknown Source)
    at javax.swing.JComponent.processKeyBinding(Unknown Source)
    at javax.swing.JComponent.processKeyBindings(Unknown Source)
    at javax.swing.JComponent.processKeyEvent(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.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown 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$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.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)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Theg8nj
  • 135
  • 1
  • 16
  • 1
    You might want to read [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – PakkuDon Jun 13 '14 at 08:56
  • 1
    `JTextField bg = new JTextField(40);` If that's what I think it is, Ugh.. use a [color chooser](http://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html). – Andrew Thompson Jun 13 '14 at 08:57

1 Answers1

3

In your constructor , you did

  JPanel mainpanel = new JPanel();

Which creates a local mainPanel and that scope is with in the constructor. You need to initialize the instance members. As of now they are null. Hence the exception.

That should be

mainpanel = new JPanel();

And same goes for remaining members text,mytext,bg..

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307