title: java.lang.NullPointerException PrintWriter
body: <p>I am getting a NullPointerException when I run my code. I have narrowed down the problem to line 38: when it is commented out the program doesn't give any errors. I just have "test" here for now.</p>
  
  <pre><code>out.println("test")
  </code></pre>
  
  <p>The run method is immediately started with the following two lines in another class</p>
  
  <pre><code>Client test = new Client();
  test.start();
  </code></pre>
  
  <p>And when a button is pressed the following code is executed which runs the sendToServer method</p>
  
  <pre><code>Client test = new Client();
  test.sendToServer(cipherText)
  </code></pre>
  
  <p>Below is the full code for my Client class.</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.Socket;
  
  public class Client extends Thread{
  
      String line = "";
      String cipherText = "";
  
      BufferedReader in;
      PrintWriter out;
  
      public void run(){
              String serverAddress = "00.000.000.000";
          try{
              Socket socket = new Socket(serverAddress, 8888);
              in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              out = new PrintWriter(socket.getOutputStream(), true);
          }
          catch(IOException e){
              e.printStackTrace();
          }
          while (true) {
              try{
                  line = in.readLine();
              }
              catch(IOException e){
                  e.printStackTrace();
              }
              System.out.println(line);
          } 
      }
  
      public void sendToServer(String in) {
          out.println("test");
      }
  
  }
  </code></pre>
  
  <p>Thanks for looking, I am still learning java so excuse any obvious mistakes.</p>