title: java.lang.NullPointerException When sending message via object output stream
body: <p>Im trying to send a message using an object. I have a bit of code that create a GUI and another chunk of code that acts either creates a server if there isnt a connection or acts as a client if a server is available. The User types in a message through the GUI and sends it via the "communicator" as a "message" object. The user is able to send messages if the communicator is setup as a client but not if its set up as a server...</p>
  
  <p>If the user tries to send a message with the program set up as a server I get the following error. </p>
  
  <p>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException</p>
  
  <p>And the message doesn't send</p>
  
  <p>This is my code for the communicator, At the bottom is the function that is supposed to send the message</p>
  
  <pre><code>package hunterinstant;
  
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.util.ArrayList;
  import java.util.Scanner;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  /**
   *
   * @author Chris
   */
  public class HunterCom implements Runnable {
  
      private static String SERVER_IP = "127.0.0.1";
      private static int SERVER_PORT = 5000;
      Scanner scanner = new Scanner(System.in);   //to read text from the console
      Socket socket = null;
      boolean client;
      Conversation window;
      ObjectOutputStream out = null;
  
      public void initialize(Conversation c, String ip, String port) {
          System.out.println(ip);
          System.out.println(port);
          SERVER_IP = ip;
          SERVER_PORT = Integer.parseInt(port);
          window = c; 
      }
  
  
  
      public void run() {
  
          try {
              socket = new Socket(SERVER_IP, SERVER_PORT);
              window.addText("Connected to server");
              client = true;
          } catch (Exception ex) {
              window.addText("No Users Online: Waiting for connection");
              client = false;
  
          }
  
          if (client == true) {
  
          ObjectInputStream in = null;
          while (true) {
              try {
                  if (out == null) {
                      out = new ObjectOutputStream(socket.getOutputStream());
                  }
  
                  //get the reply from the server
                  if (in == null) {
                      in = new ObjectInputStream(socket.getInputStream());
                  }
                  Message message = (Message) in.readObject();
  
                  window.addText(message.getMessage());
                  //System.out.println("Server said: " + message.getMessage());
  
              } catch (Exception ex) {
                  window.addText("Error: " + ex);
              }
          }
  
      } // End Client
  
          else {
              ServerSocket serverSocket = null;
              try {
                  serverSocket = new ServerSocket(SERVER_PORT);
              } catch (IOException ex) {
                  window.addText("Error occured while creating the server socket");
                  return;
              }
  
              Socket socket = null;
              try {
                  //Waits untill a connection is made, and returns that socket
                  socket = serverSocket.accept();
              } catch (IOException ex) {
                  window.addText("Error occured while accepting the socket");
                  return;
              }
              window.addText("Connection created, client IP: " + socket.getInetAddress());
              ObjectInputStream in = null;
              while (true) {
                  try {
                      if (in == null) {
                          in = new ObjectInputStream(socket.getInputStream());
                      }
  
                      Message message = (Message) in.readObject();
  
                      window.addText(message.getMessage());
                      if (out == null) {
                          out = new ObjectOutputStream(socket.getOutputStream());
                      }
                  } catch (Exception ex) {
                      window.addText("Error: " + ex);
                  }
              }
          }
      }
  
  //THIS IS THE CODE THAT IS SUPPOSED TO SEND THE MESSAGE
      public void sendMessage(String str) 
      {
          try {
              out.writeObject(new Message(str));
              System.out.println("Hello");
              out.flush();
          } catch (IOException ex) {
              Logger.getLogger(HunterCom.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  }
  </code></pre>
  
  <p>This is the code that is supposed to send the message from the GUI</p>
  
  <pre><code>private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
      comunicator.sendMessage(jTextField1.getText());
      jTextArea2.append(jTextField1.getText());
  }
  
  private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
      if(evt.getKeyCode()==10) // Has the user pressed enter?
      {
          comunicator.sendMessage(jTextField1.getText());
          jTextArea2.append(jTextField1.getText());
      }
  }
  </code></pre>
  
  <p>Sorry for what I'm sure is a noobish question... I'm sure I'm doing something really obviously stupid here but I have only been doing java for a couple of months and I have spent hours trying to figure this one out with no joy.... </p>