Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4633598

HttpURLConnection.getErrorStream() failing with java.lang.NullPointerException

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.4.0
    • core-libs
    • None
    • sparc
    • solaris_8

      When I am trying to access getErrorStream() of HttpURLConnection , its giving
      java.lang.NullPointerException in a particular condition.

      Here I have a small servlet which writes some error messages thru
      response.sendError(HttpServletResponse.SC_NOT_FOUND, errorMsg.toString());

      and a test client which accesses error stream thru
      BufferedReader bin = new BufferedReader(new InputStreamReader(ucon.getErrorStream()));

      This statement fails with the following exception :

      java HttpTestClient
      java.lang.NullPointerException
              at java.io.Reader.<init>(Reader.java:61)
              at java.io.InputStreamReader.<init>(InputStreamReader.java:55)
              at HttpTestClient.main(HttpTestClient.java:27)

      But interestingly when I access getErrorStream() after calling getResponseCode() on that connection then it works properly. I can read the error message from servlet in this case. Same servlet works fine always from browser. So I didnt see any problem from servlet side.

      I have tested this on Solaris sparc 8 with merlin build92


      HOw to reproduce:

      =================
      Attached the test servlet class and client class.
       
      Servlet class:
      ===============
      public class HttpErrStream extends HttpServlet {

       public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws IOException, ServletException
          {
              try {
                      System.out.println("IN HttpErrStream servlets do get");

                      response.setContentType("text/html");

                      StringBuffer errorMsg = new StringBuffer(" <html> <head> ");
                      errorMsg.append("<title>*** Requested resource not found *** </title>");
                      errorMsg.append("</head> <body> ");
                      errorMsg.append("<h1> ***" + HttpServletResponse.SC_NOT_FOUND + " ***</h1> ");
                      errorMsg.append("<h1> *** Error stream checking ***</h1> ");
                      errorMsg.append("</body> </html> ");

                      response.sendError(HttpServletResponse.SC_NOT_FOUND, errorMsg.toString());

       
              } catch(Exception exp) {
                      exp.printStackTrace();
              }

         } // End of doGet
      } // End of class

      Test client:
      =============

      import java.net.*;
      import java.io.*;

      public class HttpTestClient {


              public static void main(String[] args) {

                      HttpURLConnection ucon = null;
                      int responseCode;

                      try {
                              URL url = new URL("http://javacup.sfbay.sun.com:8080/examples/servlet/HttpErrStream");

                              ucon = (HttpURLConnection) url.openConnection();
                              ucon.setDoInput(true);

                              ucon.setRequestMethod("GET");

                              //ucon.connect();

                              System.out.println(" Request method" + ucon.getRequestMethod());
                              //System.out.println(" Response code " + ucon.getResponseCode());
                              BufferedReader bin = new BufferedReader(new InputStreamReader(ucon.getErrorStream()));
                              String result = bin.readLine();

                              System.out.println("read message from servlet" + result);
                              ucon.disconnect();

                      }catch(Exception exp) {
                                      exp.printStackTrace();
                      }

              } // End of main

      } // End of class


      Output from the test client in both cases:
      -------------------------------------------

      When access ucon.getResponseCode() before accessing errorstream() output is:

      java HttpTestClient
       Request methodGET
       Response code 404
      read message from servlet<html><head><title>Apache Tomcat/4.0.1 - Error report</title><STYLE><!--H1{font-family : sans-serif,Arial,Tahoma;color : white;background-color : #0086b2;} BODY{font-family : sans-serif,Arial,Tahoma;color : black;background-color : white;} B{color : white;background-color : #0086b2;} HR{color : #0086b2;} --></STYLE> </head><body><h1>Apache Tomcat/4.0.1 - HTTP Status 404 - &lt;html&gt; &lt;head&gt; &lt;title&gt;*** Requested resource not found *** &lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;h1&gt; ***404 ***&lt;/h1&gt; &lt;/body&gt; &lt;/html&gt; </h1><HR size="1" noshade><p><b>type</b> Status report</p><p><b>message</b> <u> &lt;html&gt; &lt;head&gt; &lt;title&gt;*** Requested resource not found *** &lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;h1&gt; ***404 ***&lt;/h1&gt; &lt;/body&gt; &lt;/html&gt; </u></p><p><b>description</b> <u>The requested resource ( &lt;html&gt; &lt;head&gt; &lt;title&gt;*** Requested resource not found *** &lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;h1&gt; ***404 ***&lt;/h1&gt; &lt;/body&gt; &lt;/html&gt; ) is not available.</u></p><HR size="1" noshade></body></html>

      Second case when I dont access getResponseCode() before getErrorStream():

      java HttpTestClient
       Request methodGET
      java.lang.NullPointerException
              at java.io.Reader.<init>(Reader.java:61)
              at java.io.InputStreamReader.<init>(InputStreamReader.java:55)
              at HttpTestClient.main(HttpTestClient.java:27)



       



            alanb Alan Bateman
            vakellasunw Venkata Akella (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: