title: Java TCP sockets, sending and receiving objects in a thread
body: <p>I'm a a beginner programmer, currently stuck on a java project. I have a fully working singleplayer minesweeper game, object-oriented, and I'm trying to implement a TCP multiplayer function. The game has a control class, which handles the view, game field, timer, labels, has the action listeners, etc.
  For multiplayer, I'm trying to make a separate thread, which has the Socker and the ServerSocket, and depending on the parameters passed on by the constructor, acts as a host or client. While running, it should be trying to read my custom Packet class from the input stream, and another method can send packets. Here is the code:</p>
  
  <p>public class MultiThread extends Thread {</p>
  
  <pre><code>GameControl gc;
  ServerSocket servSocket;
  Socket socket;
  Packet data;
  ObjectInputStream incoming;
  ObjectOutputStream outgoing;
  Boolean isHost;
  Boolean commEnd;
  
  public MultiThread(GameControl gc, Boolean isHost) {
      this.gc = gc;
      this.isHost = isHost;
      this.commEnd = false;
      gc.chatDialog.setVisible(true);
  }
  
  public void run() {
      try {
          if (isHost) {
              servSocket = new ServerSocket(1234);
              gc.chatDialog.status.setText("Awaiting connection...");
              socket = servSocket.accept();
              gc.chatDialog.status.setText("Connected.");
              incoming = new ObjectInputStream(socket.getInputStream());
              outgoing = new ObjectOutputStream(socket.getOutputStream());
          }
          else if (!isHost){
              socket = new Socket("localhost", 1234);
              if (socket.isConnected()) {
                  gc.chatDialog.status.setText("Connected.");
                  incoming = new ObjectInputStream(socket.getInputStream());
                  outgoing = new ObjectOutputStream(socket.getOutputStream());
              }
              else {
                  gc.chatDialog.status.setText("No connection.");
              }
          }
  
  
          while (!commEnd) {
              try {
                  Thread.sleep(100);
                  data = (Packet)incoming.readObject();
                  System.out.println(data.msg);
                  gc.chatDialog.chatArea.append(data.getMsg());   
              }
              catch (IOException e) {e.printStackTrace();}
          }
  
          System.out.println("asd");
          gc.chatDialog.status.setText("No connection");
          incoming.close();
          outgoing.close();
          socket.close();
          servSocket.close();
      }
  
          catch (Exception e) {e.printStackTrace();}
  }
  
  public void sendPacket(Packet p) {
      try {
          outgoing.writeObject(p);
      }
      catch (IOException e) {e.printStackTrace();}
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The connection gets established on localhost, but I'm getting an exception thrown when trying to initialize the incoming and outgoing fields.
  Sorry if the code is a mess, I get the feeling I'm in some serious misunderstanding, so any help is appreciated.</p>