hiddenText = ca.a.getText(); gives NullPointerException?
===

Hi everyone,
            I'm new here and new to Java programming so i will try to provide any relevant information as best i can.

My problem is i am trying to create two classes, the first class (ClassA) is a GUI, the second (ClassB) is the logic for (ClassA).

A simple example of my code is:-

**ClassA**

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

public class ClassA extends JFrame {
    
    JTextField a = new JTextField();

    JButton but = new JButton("Click");
    
    ClassB cb = new ClassB();
    
    
    public ClassA() {
        setTitle("Test");
        setLookAndFeel();
        setSize(250, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridLayout mainGrid = new GridLayout(1, 1, 20, 20);
        setLayout(mainGrid);
        
        
        add(a);
        add(but);
        but.addActionListener(cb);
        setVisible(true);
    }
    
    
    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
                    );
        } catch (Exception exc) {
            //ignore error
        }
    }
    
    public static void main(String[] args) {
        ClassA Alch = new ClassA();
    }
    
    
}

**ClassB**

    import java.awt.event.*;

public class ClassB implements ActionListener{

    String command;

    String text;

    ClassA ca;
    
    
    
    @Override
    public void actionPerformed(ActionEvent event) {
        command = event.getActionCommand();
        if (command.equals("Click")) {
        text = ca.a.getText();
        System.out.println(text);
    }
    
    }
}

The problem i am having is getting ClassB to read JTextField *a*.

If i remove the line
 
text = ca.a.getText();

and use
 
System.out.println(command);

it outputs Click so the rest of the code seems to work but as soon as i add
 
text = ca.a.getText();

i get a NullPointerException when i use the "Click" button.



So the question is how should i be writing the code to get ClassB to read the JTextField and therefore output it?