2
votes

everyone. I'm coding a function that connects to a server by using Class HttpURLConnection. In the code, I establish a connection, call getOutputStream() and getInputStream() methods in order. Then I disconnect the connection. After this, I try to get data which has been obtained by getInputStream() method, but the compiler reminds NullPointerException.

Code in below:

    DataOutputStream out = null;
    InputStreamReader inStrReader = null;
    BufferedReader reader = null;
    HttpURLConnection connection = null;

    try {
        URL postUrl = new URL(null, url, new sun.net.www.protocol.https.Handler());
        connection = (HttpURLConnection) postUrl.openConnection();
        ...//some setting methods
        connection.connect();
        out = new DataOutputStream(connection.getOutputStream());
        out.writeBytes(JSONObject.toJSONString(param));
        out.flush();
        out.close();

        inStrReader = new InputStreamReader(connection.getInputStream(), "utf-8");
        reader = new BufferedReader(inStrReader);
        connection.disconnect();   //<--HERE, release the connection
        StringBuilder stringBuilder = new StringBuilder();
        for (String line = reader.readLine(); line != null; line = reader.readLine()) {     //<--null pointer
            stringBuilder.append(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (inStrReader != null) {
            try {
                inStrReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

After debug attempts, When I move the disconnection line to the last line in finally module, everything will be ok. But I'm confused, which happens when I already assgined the 'inputstream' value to 'reader'.

Thanks a lot.

3

3 Answers

0
votes

Assigning isn't equal to reading, reader.readLine() start read from connection.

InputStreamReader is using the connection to read bytes, you disconnect before it read the bytes using the connection

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and ...

0
votes

Remember it is an "stream". You need to have an active connection to read from stream. Close the connection only after you have retrieved your data from stream.

0
votes

You're doing everything in the wrong order. It doesn't make sense.

  1. You're disconnecting and then expecting to be able to read from the connection. Total nonsense here. Normally you shouldn't disconnect at all, as you interfere with HTTP connection pooling. Just remove it, or, if you must have it, do it after all the closes.

  2. You're closing in the wrong order, but you don't need to close inStrReader at all. Closing the BufferedReader does that. Just remove all the code for inStrReader.close().

  3. You're closing out twice. Don't do that.

  4. connect() happens implicitly. You don't need to call it yourself.

  5. new URL(url) is sufficient. You haven't needed to provide the HTTPS Handler since about 2003.