title: OutputStream os = con.getOutputStream() throws NullPointerException
body: <p>I am trying to build an android app for a website and I need to post some value to this page first. 
  Here is my code:</p>
  
  <pre><code>private void sendPOST(String user,String pass) throws IOException {
  
          String POST_PARAMS = "username="+user+"&amp;password="+pass;           
          URL obj = new URL("http://xx.xx.xx.xx/mysite/test.php");          
          HttpURLConnection con = (HttpURLConnection) obj.openConnection();
          con.setRequestMethod("POST");
          con.setRequestProperty("User-Agent", "Mozilla/5.0");
  
          //----------------------------------------------------------- For POST only - START---------------------------------------------
  
           con.setDoOutput(true);
           OutputStream os = con.getOutputStream();
           os.write(POST_PARAMS.getBytes());
           os.flush();
           os.close();
  
          // ------------------------------------------------------------For POST only - END----------------------------------------------------
          int responseCode = con.getResponseCode();                   
          if (responseCode == HttpURLConnection.HTTP_OK) { //success
              BufferedReader in = new BufferedReader(new InputStreamReader(
              con.getInputStream()));
              String inputLine;
              StringBuffer response = new StringBuffer();
              while ((inputLine = in.readLine()) != null) {
                  response.append(inputLine);
              }
              in.close();
              // print result
              Toast.makeText(getApplicationContext(), response.toString()==""?"No Result":response.toString(), Toast.LENGTH_LONG).show();
          } else {
              Toast.makeText(getApplicationContext(),"POST request failed", Toast.LENGTH_LONG).show();
          }
      }
  </code></pre>
  
  <p>when this line is executed   <code>OutputStream os = con.getOutputStream();</code>
  a null exception occurs.
  I am unable to proceed further. Please suggest what i must do to remove this exception.</p>