0

I'm struggling to figure out how I can put the text I have in my textField in Frame 'Test1' into my label in Frame 'Test2'.

Please could somebody help, here is all my code:

package Frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Test1 extends JFrame {

    private JPanel contentPane;
    public static JTextField textField;



    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test1 frame = new Test1();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Test1() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.setText("yes");
        textField.setBounds(136, 98, 96, 20);
        contentPane.add(textField);
        textField.setColumns(10);
    }
}

And now the code of the 2nd frame, Test2.

package Frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

public class Test2 extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test2 frame = new Test2();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Test2() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel jlabel = new JLabel("");

    String text = Test1.textField.getText().toString();
        jlabel.setBounds(169, 113, 48, 14);
        contentPane.add(jlabel);

    }

}

The errors it comes up with is:

java.lang.NullPointerException
    at Frame.Test2.<init>(Test2.java:44)
    at Frame.Test2$1.run(Test2.java:22)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

I have tried and tried loads of different methods of doing this, and nothing seems to work. I have tried about 8 different ways and none of them work, but they always seem to work for other people. I don't understand why. Can someone please help. Hopefully my explanation is sufficient.

Thanks in advance!!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

0

To be honest, you will need to brush up various Java concepts to understand this exception. I will try to clarify a few. This can help.

  • You have textField defined as a static member of Test1 Class without a value. This essentially means that it is null.

  • In Test2 Class, you are trying to access the value of textField which has not been initialized yet as you are initializing textField in Test1 class' constructor which will not be called when an object of Test1 is created.

You have created a kind of cyclic redundancy here which can be solved in the following ways but whether it will solve your business problem or not, that is not definite:

  • Initialize the textField with declaration itself.
  • Instantiate an object of Test1 class before accessing textField's value.
Avinash Sagar
  • 527
  • 4
  • 10