The cause of your NPE is obvious:
private ChatBox box;
this is equivalent to:
private ChatBox box = null;
Then here you use box:
messagePane.append(box.getSendText()); // Here is where I am getting the NPE.
But you never give the box variable any viable object reference, and so when you try to use it it will of course be null.
But more importantly, how are you trying to connect these two classes because I see no code that does anything of this sort.
The solution is to first think how you want objects to be connected, to communicate with each other, and then write the code so that this will happen. Perhaps you want to create a new ChatBox object in your MessageWindow class:
private ChatBox box = new ChatBox();
but if one already exists and is already part of a GUI, then you don't in fact want to do this. If one already exists, what you'll instead want to do is to give MessageWindow a setChat(ChatBox chat)...
method that sets the reference, either that or pass in the ChatBox reference via the MessageWindow class's constructor:
public MessageWindow(ChatBox chat) {
this.chat = chat;
// ... plus other constructor-specific code
}
Also, as has been mentioned in comments, you should not be using a KeyListener here, and as a general rule (that is occasionally broken) should avoid using these constructs in most of your Swing programs.
Rather than a JTextArea that uses a KeyListener, I'd suggest that you use a JTextField that uses an ActionListener to accept input text. For example, please have a look at my code in my answer to this question.
Edit 2
Modification of code from my previous answer to show two chat windows communicating with each other:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.text.JTextComponent;
@SuppressWarnings("serial")
public class TerminalForm extends JPanel {
private static final int GAP = 3;
private JTextArea textarea;
private JTextField textfield;
private String userName;
public TerminalForm(String userName, int rows, int cols, InputStream inStream,
PrintStream printStream) {
this.userName = userName;
textarea = prepareTextArea(rows, cols, inStream);
textfield = prepareTextField(cols, printStream, textarea);
setLayout(new BorderLayout(GAP, GAP));
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
add(new JScrollPane(textarea), BorderLayout.CENTER);
add(textfield, BorderLayout.SOUTH);
}
public String getUserName() {
return userName;
}
private JTextField prepareTextField(int cols, PrintStream printStream,
JTextArea textArea) {
JTextField textField = new JTextField(cols);
textField.addActionListener(new TextFieldListener(printStream, textArea));
return textField;
}
private JTextArea prepareTextArea(int rows, int cols, InputStream inStream) {
JTextArea textArea = new JTextArea(rows, cols);
textArea.setEditable(false);
textArea.setFocusable(false);
InputStreamWorker instreamWorker = new InputStreamWorker(textArea,
inStream);
instreamWorker.execute();
return textArea;
}
private class TextFieldListener implements ActionListener {
private PrintStream printStream;
private JTextArea textArea;
public TextFieldListener(PrintStream printStream, JTextArea textArea) {
this.printStream = printStream;
this.textArea = textArea;
}
@Override
public void actionPerformed(ActionEvent evt) {
JTextComponent textComponent = (JTextComponent) evt.getSource();
String text = textComponent.getText();
textComponent.setText("");
printStream.println(userName + "> " + text);
textArea.append(userName + "> " + text + "\n");
}
}
private class InputStreamWorker extends SwingWorker<Void, String> {
private Scanner scanner;
private JTextArea textArea;
private InputStreamWorker(JTextArea textArea, InputStream inStream) {
this.textArea = textArea;
scanner = new Scanner(inStream);
}
@Override
protected Void doInBackground() throws Exception {
while (scanner.hasNextLine()) {
publish(scanner.nextLine());
}
return null;
}
@Override
protected void process(List<String> chunks) {
for (String chunk : chunks) {
textArea.append(chunk + "\n");
}
}
}
private static void createAndShowGui(String userName, final InputStream inStream,
final PrintStream printStream) {
int rows = 20;
int cols = 40;
TerminalForm mainPanel = new TerminalForm(userName, rows, cols, inStream,
printStream);
JFrame frame = new JFrame("Terminal Form: " + userName);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
final PipedOutputStream outStream1 = new PipedOutputStream();
final PipedInputStream inStream1 = new PipedInputStream();
try {
final PipedOutputStream outStream2 = new PipedOutputStream(inStream1);
final PipedInputStream inStream2 = new PipedInputStream(outStream1);
createAndShowGui("John", inStream1, new PrintStream(outStream1));
createAndShowGui("Fred", inStream2, new PrintStream(outStream2));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Run the code and type into both chat JTextFields to see what I mean.
Edit 3
Key Bindings version
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.Scanner;
import javax.swing.*;
@SuppressWarnings("serial")
public class TerminalFormWithTextArea extends JPanel {
private static final int GAP = 3;
private static final int ENTER_TEXT_AREA_ROWS = 3;
private JTextArea textarea;
private JTextArea enterTextArea;
private String userName;
public TerminalFormWithTextArea(String userName, int rows, int cols, InputStream inStream,
PrintStream printStream) {
this.userName = userName;
textarea = prepareTextArea(rows, cols, inStream);
enterTextArea = prepareEnterTextArea(cols, printStream, textarea);
setLayout(new BorderLayout(GAP, GAP));
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
add(new JScrollPane(textarea), BorderLayout.CENTER);
add(new JScrollPane(enterTextArea), BorderLayout.SOUTH);
}
public String getUserName() {
return userName;
}
private JTextArea prepareEnterTextArea(int cols, PrintStream printStream,
JTextArea textArea) {
JTextArea enterTxtArea = new JTextArea(ENTER_TEXT_AREA_ROWS, cols);
enterTxtArea.setWrapStyleWord(true);
enterTxtArea.setLineWrap(true);
// textField.addActionListener(new TextFieldListener(printStream, textArea));
int condition = JComponent.WHEN_FOCUSED;
InputMap inputMap = enterTxtArea.getInputMap(condition);
ActionMap actionMap = enterTxtArea.getActionMap();
KeyStroke enterKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
String enter = "enter";
inputMap.put(enterKeyStroke, enter);
actionMap.put(enter, new EnterAction(printStream, enterTxtArea, textArea));
return enterTxtArea;
}
private JTextArea prepareTextArea(int rows, int cols, InputStream inStream) {
JTextArea textArea = new JTextArea(rows, cols);
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
InputStreamWorker instreamWorker = new InputStreamWorker(textArea,
inStream);
instreamWorker.execute();
return textArea;
}
private class EnterAction extends AbstractAction {
private PrintStream printStream;
private JTextArea enterTxtArea;
private JTextArea textArea;
public EnterAction(PrintStream printStream, JTextArea enterTextArea,
JTextArea textArea) {
this.printStream = printStream;
this.enterTxtArea = enterTextArea;
this.textArea = textArea;
}
@Override
public void actionPerformed(ActionEvent evt) {
String text = userName + "> " + enterTxtArea.getText();
enterTxtArea.setText("");
printStream.println(text);
textArea.append(text + "\n");
}
}
private class InputStreamWorker extends SwingWorker<Void, String> {
private Scanner scanner;
private JTextArea textArea;
private InputStreamWorker(JTextArea textArea, InputStream inStream) {
this.textArea = textArea;
scanner = new Scanner(inStream);
}
@Override
protected Void doInBackground() throws Exception {
while (scanner.hasNextLine()) {
publish(scanner.nextLine());
}
return null;
}
@Override
protected void process(List<String> chunks) {
for (String chunk : chunks) {
textArea.append(chunk + "\n");
}
}
}
private static void createAndShowGui(String userName, final InputStream inStream,
final PrintStream printStream) {
int rows = 20;
int cols = 40;
TerminalFormWithTextArea mainPanel = new TerminalFormWithTextArea(userName, rows, cols, inStream,
printStream);
JFrame frame = new JFrame("Terminal Form: " + userName);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
final PipedOutputStream outStream1 = new PipedOutputStream();
final PipedInputStream inStream1 = new PipedInputStream();
try {
final PipedOutputStream outStream2 = new PipedOutputStream(inStream1);
final PipedInputStream inStream2 = new PipedInputStream(outStream1);
createAndShowGui("John", inStream1, new PrintStream(outStream1));
createAndShowGui("Fred", inStream2, new PrintStream(outStream2));
} catch (IOException e) {
e.printStackTrace();
}
}
}