title: Java, "URI can't be null" error on simple HTTP client
body: <p>I'm trying to write a simple HTTP client that, given an absolute HTTP path, performs a simple HTTP GET request and prints out the content to standard output.</p>
  
  <p>However, I'm getting the following error on any page I try to load: </p>
  
  <pre><code>    java.lang.IllegalArgumentException: URI can't be null.
      at sun.net.spi.DefaultProxySelector.select(DefaultProxySelector.java:141)
      at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:926)
      at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:850)
      at HTTrack.main(HTTrack.java:68)
  </code></pre>
  
  <p>The code that is raising the exception is:</p>
  
  <pre><code>try
  {
      System.out.println("We will attempt to read " + getfilepath(args[0]) + " from " + getbasesite(args[0]));
      URL serverConnect = new URL("http", getbasesite(args[0]), getfilepath(args[0]));
      con = (HttpURLConnection)serverConnect.openConnection();
      con.setRequestMethod("GET");    
      con.setDoOutput(true);
      con.setReadTimeout(10000);
      System.out.println("Attempting to connect to " + getbasesite(args[0]) + " on port 80...");
      System.out.println("URL = " + con.getURL());
      con.connect();
      //System.out.println("Connection succeeded! Attempting to read remote file " + getfilepath(args[0]));
      BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
      String line = "";
      while((line = br.readLine()) != null)
      {
          System.out.println(line);
      }
  
  }
  </code></pre>
  
  <p>The line that is raising the exception <code>(HTTrack.java:68)</code> is the <code>con.connect()</code> line, and the functions <code>getbasesite</code> and <code>getfilepath</code> return the server host name and remote file paths of a URL, respectively.</p>
  
  <p>For example if passed the string <code>http://www.somesite.com/somepage.html</code>, <code>getbasesite</code> will return <code>"www.somesite.com"</code> and <code>getfilepath</code> will return <code>"/somepage.html"</code>.  I know the <code>HttpURLConnection</code> is being passed these values correctly because when I call <code>getURL</code> it returns what I expect: <code>http://www.somesite.com/somepage.html</code></p>
  
  <p>I'm stuck as to what might be causing this error - I've tested that the <code>HttpURLConnection</code> class indeed gets the correct URL out of the constructor arguments with the line <code>System.out.println("URL = " + con.getURL());</code>, so I'm not sure why it's failing the attempts to connect with the error that the "URI can't be null".</p>