Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing.getText() String to another class

I'm currently working on a first year university project and I have a problem, although I doubt it's a very complicated one, but I've been searching and I just can't find a suitable answer to it.

The problem concerns two classes. A gui class (class1) and another class (class2), when I run the code the second class gives me a nullpointerexception when trying to retrieve the getText() String in class2 from class1.

I have a JTextField in class1 and am trying to pass through the .getText() value to class2 and store it in a String type variable.

The current code I'm trying to achieve this with is the following (simplified):

 public class Class1 {

    private JTextField textField = new JTextField("Something");

      public Class1() {}
    ...
    public String getTextFieldString() {
      return textField.getText();
  }
}

 public class Class2 {

   public Class2(Class1 class1) {
    c1=class1
   }
    private c1 Class1 = new Class1();
    private String s = new String();
    ...
    s = c1.getTextFieldString();
}

I'm pretty new to coding, I've read that maybe I need to pass through an argument somewhere and I assume that's because textField is not static in itself, it changes when somebody enters a new value. (sorry for stating the obvious there.)

Anyway, help is appreciated. Thanks a lot!

like image 947
DanMc Avatar asked Jan 16 '23 23:01

DanMc


2 Answers

I may help to review the prescribed initialization order in Java. In particular, the textField instance variable initializer should be complete by the time a valid Class1 instance exists. One way to violate this order is to construct Class1 and Class2 on different threads. To preclude this, Swing GUI objects should be constructed and manipulated only on the event dispatch thread, as shown in the sscce below.

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/** @see http://stackoverflow.com/q/10067399/230513 */
public class NewJavaGUI extends JPanel {

    public NewJavaGUI() {
        Class1 class1 = new Class1();
        Class2 class2 = new Class2(class1);
        this.add(class1.textField);
        this.add(new JLabel(class2.s));
    }

    private static class Class1 extends Object {

        private final JTextField textField = new JTextField("Something");

        public Class1() {} // implicit

        public String getTextFieldString() {
            return textField.getText();
        }
    }

    private static class Class2 extends Object {

        private final Class1 c1;
        private final String s;

        public Class2(Class1 class1) {
            c1 = class1;
            s = c1.getTextFieldString();
        }
    }

    private void display() {
        JFrame f = new JFrame("NewJavaGUI");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new NewJavaGUI().display();
            }
        });
    }
}
like image 170
trashgod Avatar answered Jan 28 '23 11:01

trashgod


Well, you could pass a reference to Class1 in the constructor of Class2 and save it in an attribute, and every time Class2 needs the text, simply call the getText() method on the attribute.

public class Class2 {
    private Class1 class1;
    public Class2(Class1 reference) {
        class1 = reference;
    }
    public void someMethod() {
        String s = class1.getTextFieldString();
    }
}

Now, when you instantiate a Class2 object:

Class1 c1 = new Class1();
Class2 c2 = new Class2(c1);
like image 30
Óscar López Avatar answered Jan 28 '23 13:01

Óscar López