title: PrintWriter giving null pointer exception
body: <p>I am trying to build a multiple client single java server model. I'm doing this using threads, so I have one thread per client using <code>AsyncTask</code>. Thus, I am creating a client socket and the <code>PrintWriter</code> object in the <code>onCreate()</code> method using the <code>CreateClient</code> class and then every time the <code>button2</code> is pressed, I just need to pass the question/text to the server for that particular thread. The problem is, I am getting a <code>NullPointerException</code> in the <code>SendMessage</code> class.</p>
  
  <p>Here's my Client Actvity: </p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Locale;
  
  //import statements for client
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.net.Socket;
  import java.net.UnknownHostException;
  
  import android.speech.RecognizerIntent;
  import android.speech.tts.TextToSpeech;
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.Window;
  import android.view.WindowManager;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ImageButton;
  import android.widget.Toast;
  
  //import statements for client
  import android.os.AsyncTask;
  
  
  public class MainActivity extends Activity implements OnClickListener {
  
      EditText question;
  
      //Client sockets
      private Socket client;
      private PrintWriter printwriter;
      private String toTag;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_main);
  
          // create client as soon as the activity starts
          CreateClient clientConnection = new CreateClient();
          clientConnection.execute();
  
          question = (EditText)findViewById(R.id.editText1);
  
          Button query = (Button)findViewById(R.id.button2);
          query.setOnClickListener(this);
  
          ImageButton listen = (ImageButton)findViewById(R.id.button1);
          listen.setOnClickListener(this);
          listen.performClick();
  
      }
  
      private class CreateClient extends AsyncTask&lt;Void, Void, Void&gt; {
  
          @Override
          protected Void doInBackground(Void... params) {
  
              try {
                  client = new Socket("Server IP Address", 4444);
  
                  printwriter = new PrintWriter(client.getOutputStream(), true);
              } catch (UnknownHostException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();    
              }
              return null;
          }
  
      }
  
      private class CloseClient extends AsyncTask&lt;Void, Void, Void&gt; {
  
          @Override
          protected Void doInBackground(Void... params) {
  
              if(printwriter != null) {
                  try {
                      printwriter.write("\n");
                      printwriter.close();
                      client.close(); // closing the connection
  
                  } catch (UnknownHostException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
  
              return null;
          }
  
      }
  
      private class SendMessage extends AsyncTask&lt;Void, Void, Void&gt; {
  
          @Override
          protected Void doInBackground(Void... params) {
  
                  printwriter.write(toTag); // GETTING NULL POINTER EXCEPTION ON THIS LINE
                  printwriter.write("\n"); //delimiter
  
                  printwriter.flush();
  
              return null;
          }
  
      }
  
      public void onClick(View v)
      {
          switch(v.getId())
          {
              case R.id.button2:
                  if(validate()) //Validate is a function that validates the text
                  {
                      toTag = question.getText().toString();
  
                      //Invoke the execute method of AsynTask, which will run the doInBackground method of SendMessage Class
  
                      SendMessage sendMessageTask = new SendMessage();
                      sendMessageTask.execute();
  
                  }
                  else
                  {
                      //Toast error msg
                  }
  
                  break;
          }
  
      }
  
      @Override
      public void onBackPressed() {
          super.onBackPressed();
          CloseClient closeConnection = new CloseClient();
          closeConnection.execute();
      }
  
  }
  </code></pre>