title: Thread runs fine when I use it in onCreate but App crashes when I start Thread onClick?
body: <p>I have a cards game for LAN play. The code works fine on PC but now I shifted my code to Android. I'm getting a weird problem.</p>
  
  <p>I'm hosting a game on a Thread by starting it on Button Click which takes no. of players. Heres the onClick method:</p>
  
  <pre><code> public void onClick(View v) {
              if(txtPlayers.getText().toString()!=null){
                  int pl=Integer.parseInt(txtPlayers.getText().toString());
                  Toast.makeText(MainActivity.this,"Server Started",Toast.LENGTH_LONG).show();
                  new Thread(new GameServer(pl)).start();
              }
              else
              {
                  Toast.makeText(MainActivity.this,"Enter Players",Toast.LENGTH_LONG).show();
              }
          }
  </code></pre>
  
  <p>In the above code I start a runnable Interface of the GameServer which goes like this:</p>
  
  <pre><code>public class GameServer implements Runnable {
  private ServerSocket serverSocket = null;
  private Socket clientSocket = null;
  private int maxClientsCount,port;
  private  ClientThread[] threads;
  private  Turn turn;
  private  SumGame sg;
  public final String TAG = "com.codechefamit.min";
  
  GameServer(int players){
      maxClientsCount=players;
      port=7777;
      turn=new Turn(maxClientsCount);
      sg=new SumGame(maxClientsCount);
      threads=new ClientThread[maxClientsCount];
  }
  
  public void run() {
      try {
          Log.i(TAG,"Listening.....");
          serverSocket = new ServerSocket(port);
      } catch (IOException e) {
          System.out.println(e);
      }
      int count=0;
      while (count&lt;maxClientsCount) {
          try {
              clientSocket = serverSocket.accept();
              int i = 0;
              for (i = 0; i &lt; maxClientsCount; i++) {
                  if (threads[i] == null) {
                      threads[i] = new ClientThread(clientSocket, threads,turn,count+1,sg);
                      count++;
                      Log.i(TAG,count+" Connected");
                      break;
                  }
              }
          } catch (IOException e) {
              System.out.println(e);
          }
      }
      Log.i(TAG,"Starting Clients");
      for (int i = 0; i &lt; maxClientsCount; i++)
          threads[i].start();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>GameServer starts a new Thread for every Client Request. This code runs fine when I start GameServer Thread in My MainActivity onCreate() but when I copied the same code to a Button Click my App crashes.</p>
  
  <p>The Client who hosts the game also joins the game from MainActivity. This works when I start GameServer thread inside onCreate. What is it that I'm doing wrong?</p>
  
  <pre><code>01-12 20:34:21.252  22250-22741/? E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-2419
  java.lang.NullPointerException
          at com.codechefamit.min_sumandroidclient.ClientThread.run(ClientThread.java:57)
  </code></pre>