title: Client-Server Socket Programming Java
body: <p>I'm new to socket programming and i'm trying to get my client-program to retrieve a url from a document on my desktop, send this url to my server-program, and get my server-program to send back the html code of this url-website as a string:
  I keep getting a NPE when i try to send back the string to my client, can anyone point me to what i'm doing wrong?</p>
  
  <p>Server-Program:</p>
  
  <pre><code> public class Server {
   public static void main(String args[]) {
      String fromClient;
      StringBuilder htmlCode = null;
      String str = null;
      try(ServerSocket welcomeSocket = new ServerSocket(8082)){
          System.out.println("Server started, waiting for clients...");
          while(true){
              try(Socket connectionSocket = welcomeSocket.accept();
                  BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                  DataOutputStream toClient = new DataOutputStream(connectionSocket.getOutputStream())){
                  fromClient = inFromClient.readLine();
                  try(InputStream url = new URL("http://"+fromClient+"/index.html").openStream();
                      BufferedReader getUrl = new BufferedReader(new InputStreamReader(url))){
                          for(String line = getUrl.readLine(); line != null; line = getUrl.readLine()){
                              //THIS IS WHERE ECLIPSE SAYS NPE IS HAPPENING
                              htmlCode = htmlCode.append(line);
                          }
                      str = htmlCode.toString();
                      toClient.writeBytes(str);   
                  }
              }
          }
      }
      catch(IOException io){
          io.printStackTrace();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Client-Side:</p>
  
  <pre><code> public class Client {
   public static void main(String[] args) {
      String url;
      String returned;
      try(Socket clientSocket = new Socket("localhost", 8082);
          BufferedReader inFromUser = new BufferedReader(new FileReader("C:\\Users\\Anthony\\Desktop\\client.txt"));
          DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
          BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))){
  
          url = inFromUser.readLine();
          outToServer.writeBytes(url + "\n");
          returned = inFromServer.readLine();
          System.out.println("FROM SERVER: " + returned);
      }
      catch(IOException io){
          io.printStackTrace();
      }
  }
  </code></pre>
  
  <p>}</p>