Search code examples
javaswinguser-interfaceactionlistenerjtextfield

Java GUI Variables Problems


I seem to have set up something wrong in the action listener for the Create Button handler. When I tried to get the value of the text field nameTF I get a null pointer error. I tried to imitate your code for the calculator, and the Exit Button Handler works well. I know that pressing the Create button invokes the handler but the statement

inName = nameTF.getText();

gives the error message.

The complete text of the listener is

private class CreateButtonHandler 
    implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
        String inName, inType; //local variables
        int inAge;
        Dog arf;
        inName = nameTF.getText();
        inType = typeTF.getText();
        inAge = Integer.parseInt( ageTF.getText() );
        //System.out.println("Inside CreateButtonHandler");
        System.out.println(inName);
        arf = new Dog(inName, inType, inAge);
        System.out.println(arf.toString () );

    }

}

and the whole program is below. Any help/explanation/suggestion would be very welcome.

import javax.swing.*; // import statement for the GUI components
import java.awt.*; //import statement for container class
import java.awt.event.*;



public class DogGUI //creation of DogGUI clas
{
private JLabel nameL, typeL, ageL, outtputL;
private JTextField nameTF, typeTF, ageTF;
private JButton createB, exitB;
CreateButtonHandler cbHandler;
ExitButtonHandler ebHandler; 

    public void driver () //creates everything
    {
        //create the window
        JFrame DogInfo = new JFrame ("Dog GUI");
        DogInfo.setSize(400,300); //set the pixels for GUI
        DogInfo.setVisible(true); // set visibility to true
        DogInfo.setDefaultCloseOperation(DogInfo.EXIT_ON_CLOSE); // when closed JFrame will disappear

//layout
Container DogFields = DogInfo.getContentPane();
DogFields.setLayout(new GridLayout(5,2));

// setting labels for GUI
nameL = new JLabel ("Enter name of Dog:  ",
                    SwingConstants.RIGHT);
typeL = new JLabel ("Enter the type of the Dog:  ",
                    SwingConstants.RIGHT);
ageL = new JLabel ("Enter the age of the Dog:   ",
                    SwingConstants.RIGHT);
outtputL = new JLabel ("Dog Information:  ",
                    SwingConstants.RIGHT);

//Buttons

JButton createB, exitB; //creating button for creation of Dog and button to exit
createB = new JButton("Create Dog");
exitB = new JButton("Exit");

//text fields

JTextField nameTF, typeTF, ageTF, outtputTF; 
nameTF = new JTextField(10);
typeTF = new JTextField(15);
ageTF = new JTextField(5);
outtputTF = new JTextField(25);

outtputTF.setEditable(false); //this TF is to display this output

//Lables and Textfields 
DogInfo.add(nameL);
DogInfo.add(nameTF);

DogInfo.add(typeL);
DogInfo.add(typeTF);

DogInfo.add(ageL);
DogInfo.add(ageTF);

DogInfo.add(outtputL);
DogInfo.add(outtputTF);

//Buttons
DogInfo.add(createB);
DogInfo.add(exitB);


//Instantiate Listeners
cbHandler = new CreateButtonHandler();
ebHandler = new ExitButtonHandler();

//Register Listeners
createB.addActionListener(cbHandler);
exitB.addActionListener(ebHandler);

DogInfo.setVisible(true);
}
//run the program from main, instantiate class, invoke driver() method

public static void main(String[] args)
{
    DogGUI d = new DogGUI();
    d.driver();
}

// class to actually handle Dog creation

private class CreateButtonHandler 
    implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
        String inName, inType; //local variables
        int inAge;
        Dog arf;
        inName = nameTF.getText();
        inType = typeTF.getText();
        inAge = Integer.parseInt( ageTF.getText() );
        //System.out.println("Inside CreateButtonHandler");
        System.out.println(inName);
        arf = new Dog(inName, inType, inAge);
        System.out.println(arf.toString () );

    }

}


private class ExitButtonHandler 
    implements ActionListener
{
    public void actionPerformed(ActionEvent e)
        {
            System.out.println("inside exit button");
            System.exit(0);
        } // end method actionPerformed
}


}

Solution

  • You have a local variable in the driver() method called nameTF that is hiding that field (same for some other variables).

    JTextField nameTF, typeTF, ageTF, outtputTF; 
    nameTF = new JTextField(10);
    

    Remove the declaration of those fields since they are already declared as instance fields.

    private JTextField nameTF, typeTF, ageTF;
    

    (probably not outtputTF, depending on what you want to do)