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();}
}
}
}
okay here is you problem. You have created the object of
GUIin theServerLoginGUIclass. But you are not initializing yourGUiobject with the reference of calling class. Here is what you need to do to fix this. To yourServerLoginGUIclass add the following constructor: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. 🙂