Mubs123
Mubs123

Reputation: 35

Values user inputs into a GUI are not saving?

Below is the code I am working on. Basically I need a box to open and get the user to input their data in the corresponding text fields. All is working great. Everything is outputting correctly when I print it from the actionPerformed method, but when I call gui.displayPersonInfo method from main it shows all of the values as null. It is doing the displayPersonInfo method first before the box has even opened, even though I call the method after. Anyone know what is wrong with my code? (output below)

package userInput;

import javax.swing.JFrame;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Person extends JFrame{

String Name; 
String Address;
String PhoneNumHome;
String PhoneNumWork;
String email;

JLabel label, label2;
JTextField tf1, tf2, tf3, tf4, tf5, tf6, tf7, tf8;
JButton button;

public Person(){
    setLayout(new FlowLayout());
    label = new JLabel("Enter your name");
    add(label);
    tf1 = new JTextField(10);
    add(tf1);

    label = new JLabel("Enter your Address (street number + street name)");
    add(label);
    tf2 = new JTextField(10);
    add(tf2);

    label = new JLabel("Enter your city");
    add(label);
    tf3 = new JTextField(10);
    add(tf3);

    label = new JLabel("Enter your province");
    add(label);
    tf4 = new JTextField(10);
    add(tf4);

    label = new JLabel("Enter your postal code");
    add(label);
    tf5 = new JTextField(10);
    add(tf5);

     label = new JLabel("Enter your home phone number (306-xxx-xxx)");
    add(label);
    tf6 = new JTextField(10);
    add(tf6);

     label = new JLabel("Enter your work phone number (306-xxx-xxx)");
    add(label);
    tf7 = new JTextField(10);
    add(tf7);

     label = new JLabel("Enter your email ([email protected]");
    add(label);
    tf8 = new JTextField(10);
    add(tf8);

    button = new JButton("Next");
    add(button);

    event e = new event();
    button.addActionListener(e);
}

public class event implements ActionListener{
    public void actionPerformed(ActionEvent e){
        String address1, pnum, wnum, a;
        try{
            String word = tf1.getText();
            Name = word;
            System.out.println(Name);
            Address = tf2.getText();
            Address = Address + " " + tf3.getText();
            Address = Address + " " + tf4.getText();
            Address = Address + " " + tf5.getText();
            address1 = Address;
            System.out.println(Address);
            PhoneNumHome = tf6.getText();
            pnum = PhoneNumHome;
            PhoneNumWork = tf7.getText();
            wnum = PhoneNumWork;
            email = tf8.getText();
            a = email;
            System.out.println(PhoneNumHome);
            System.out.println(PhoneNumWork);
            System.out.println(email);

            saveInfo(word, address1, pnum, wnum, a);
            displayPersonInfo();
            System.exit(0);

        }catch(Exception ex){}
    }
}

public void displayPersonInfo(){
    System.out.println("Name: " + Name);
    System.out.println("Address: " + Address);
    System.out.println("Home Phone Number: " + PhoneNumHome);
    System.out.println("Work Phone Number: " + PhoneNumWork);
    System.out.println("Email: " + email);
}

public void saveInfo(String name, String address, String Hphone, String Wphone, String Email){
    Name = name;
    Address = address;
    PhoneNumHome = Hphone;
    PhoneNumWork = Wphone;
    email = Email;
}

public static void main(String[] args) {
    Person gui = new Person();

    gui.displayPersonInfo();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setTitle("Enter Information");
    gui.setSize(350,330);
    gui.setLocation(500,250);
    gui.setVisible(true);

}

}

here is the output: (it acts as if displayPersonInfo occurs first)
run:
Name: null
Address: null
Home Phone Number: null
Work Phone Number: null
Email: null
A Name (now it prints it from within actionPerformed) An Adress a City a province a postal code
a number
another number
an email
Name: A Name
Address: An Adress a City a province a postal code
Home Phone Number: a number
Work Phone Number: another number
Email: an email
BUILD SUCCESSFUL (total time: 20 seconds)

Upvotes: 0

Views: 1499

Answers (2)

Ericson Willians
Ericson Willians

Reputation: 7845

In the main method the "displayPersonInfo()" method is being called right on start. You declared the string name as a field up there uninitialized, so by default its value is null. You initialize this variable inside the actionPerformed() method inside the event class.

The problem is:

public void displayPersonInfo() {
    System.out.println("Name: " + Name);
}

public static void main(String[] args) {
    Person gui = new Person();
    gui.displayPersonInfo();
}

You attribute the value that you want for the variable ONLY when the action is performed, so if you try to access that variable before that you will the get the default value up there, which is null. And that's exactly what you're doing. When the main method starts, it calls the displayPersonInfo(), and inside of that you try to access the variable name and it returns to you null, because the variable only receives the value that you want when the action is performed.

So the solution would be:

public void displayPersonInfo() {
    String word = tf1.getText();
    Name = word;
    System.out.println("Name: " + Name);
}

You must give the value that you want before you call the variable. The same applies to the other ones. If you declare something as "String Name;" and try to call it, you will receive null.

Upvotes: 1

Steven Huang
Steven Huang

Reputation: 509

It seems your saveInfo function is redundant since you already set every value prior to calling it, but I don't know why saveInfo is nulling the values. Try to call displayPersonInfo before saveInfo and see what happens.

Upvotes: 1

Related Questions