title: getOutputStream on a null socket?
body: <p>I'm new on the site and I'm here to ask a question about Sockets and Thread. I have this code, and it gives me the error below:</p>
  
  <pre><code>    public class Conferma extends AppCompatActivity {
  
      private final String TAG = "Conferma";
      public Socket socket;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.conferma);
  
          final EditText etTavolo = (EditText) findViewById(R.id.etTavolo);
          final RadioButton rbSi = (RadioButton) findViewById(R.id.rbSi);
          RadioButton rbNo = (RadioButton) findViewById(R.id.rbNo);
          Button bInvia = (Button) findViewById(R.id.bInvia);
  
          Thread t = new Thread(new ClientThread());
          t.start();
  
          bInvia.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  String ordine = getIntent().getExtras().getString("ordine");
                  if(etTavolo.getText().toString().equals("")){
                      AlertDialog.Builder builder = new AlertDialog.Builder(Conferma.this);
                      builder.setMessage("Inserire il numero del tavolo!").setNegativeButton("Riprova!", null).create().show();
                  }
                  ordine = ordine + "Tavolo: " + etTavolo.getText().toString() + "/";
                  if(rbSi.isChecked()) {
                      ordine = ordine + "Coperto: Sì/";
                  }
                  else{
                      ordine = ordine + "Coperto: No/";
                  }
                  Log.d(TAG, ordine);
  
                  //invio informazione
                  inviaStringa(view, ordine);
              }
          });
      }
  
      private void inviaStringa(View view, String ordine) {
  
          try{
              OutputStream s = socket.getOutputStream();
              OutputStreamWriter outputStreamWriter = new OutputStreamWriter(s);
              BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
              PrintWriter out = new PrintWriter(bufferedWriter, true);
              out.println(ordine);
          } catch (Exception e) {
              Log.e(TAG, "Errore: " + e);
          }
      }
  
      private class ClientThread implements Runnable {
  
          private static final int SERVER_PORT = 6000;
          private static final String SERVER_IP = "109.115.84.90";
          private static final String TAG = "ClientThread";
  
          @Override
          public void run() {
              try {
                  InetAddress serverAddress = InetAddress.getByName(SERVER_IP);
                  Log.d(TAG, "creato");
                  socket = new Socket(serverAddress, SERVER_PORT);
              } catch (UnknownHostException e) {
                  Log.e(TAG, "Errore: " + e);
              } catch (IOException e) {
                  Log.e(TAG, "Errore: " + e);
              }
          }
      }
  }
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.OutputStream java.net.Socket.getOutputStream()' on a null object reference</p>
  </blockquote>
  
  <p>Now, from the error i supposed that the problem is in the Socket, which means that is not initialized. But if i call the ClientThread method inside the creation of the simple Thread, why don't he get initialized? Thanks for the help, and sorry if I can't be more clear than this! :((</p>