Jhubbert
Jhubbert

Reputation: 35

Java setText() Error

I am not understanding why I am getting a runtime error when I try to .setText() to a JTextArea Object in my program. In my main GUI class I have set up a event that creates a pop up JFrame object, this JFrame has a button in it which is set up too do a JTextArea.setText(); to a JTextArea in my main GUI class called MainOut.

public class GUI extends JFrame implements ActionListener {



JTextArea MainOut       =       new JTextArea(20,50);


public void actionPerformed(ActionEvent e) {


        if (e.getSource() == ExitVar){
            System.exit(0);
        }

        else if (e.getSource() == ServerLoginVar) { //This is my event that creates a 
                                                            //new JFrame popup
            new ServerLoginGUI(this);   
        } 

//-------------------------------------------------------------------
public class ServerLoginGUI extends JFrame implements ActionListener {

    JTextField ServerIP             = new JTextField(15);
    JPasswordField ServerPassword       = new JPasswordField(15);
    JPanel ServerLoginPanel         = new JPanel();
    JButton LoginButton         = new JButton("Login");
    JTextArea Area;
    JLabel ServerIPLabel            = new JLabel("Server Address:");
    JLabel ServerPasswordLabel      = new JLabel("Password      :");
    GUI GUi;
   public void actionPerformed(ActionEvent e) {

        if (e.getSource() == LoginButton){
            if (ServerIP.getText().isEmpty() ||  ServerPassword.getText().isEmpty()){
                } //do nothing

            else {
                new ServerAccess(this);

// this is the .setText() that will generate a error

                GUi.SiteNameField.setText("Test from the ServerLogin event!");

                dispose();}
                    }
        }

}

Upvotes: 0

Views: 1468

Answers (1)

Shaunak
Shaunak

Reputation: 18028

okay here is you problem. You have created the object of GUI in the ServerLoginGUI class. But you are not initializing your GUi object with the reference of calling class. Here is what you need to do to fix this. To your ServerLoginGUI class add the following constructor:

public ServerLoginGUI(GUI gui)
{
  this.GUi = gui;
}

Now your code should work fine and not give a run time error. Which I am assuming is a nullpointer error though you have not specified.

PS: Please get the Java conventions right. Variables start with letter in lower case. :)

Upvotes: 1

Related Questions