Index: trunk/src/org/openstreetmap/josm/io/OsmApi.java
===================================================================
--- trunk/src/org/openstreetmap/josm/io/OsmApi.java	(revision 2477)
+++ trunk/src/org/openstreetmap/josm/io/OsmApi.java	(revision 2478)
@@ -473,4 +473,7 @@
      *    but including any object ids (e.g. "/way/1234/history").
      * @param requestBody the body of the HTTP request, if any.
+     * @param monitor the progress monitor
+     * @param doAuthenticate  set to true, if the request sent to the server shall include authentication
+     * credentials;
      *
      * @return the body of the HTTP response, if and only if the response code was "200 OK".
@@ -499,5 +502,5 @@
 
                     // It seems that certain bits of the Ruby API are very unhappy upon
-                    // receipt of a PUT/POST message withtout a Content-length header,
+                    // receipt of a PUT/POST message without a Content-length header,
                     // even if the request has no payload.
                     // Since Java will not generate a Content-length header unless
@@ -535,10 +538,15 @@
                     i = activeConnection.getErrorStream();
                 }
-                BufferedReader in = new BufferedReader(new InputStreamReader(i));
-
-                String s;
-                while((s = in.readLine()) != null) {
-                    responseBody.append(s);
-                    responseBody.append("\n");
+                if (i != null) {
+                    // the input stream can be null if both the input and the error stream
+                    // are null. Seems to be the case if the OSM server replies a 401
+                    // Unauthorized, see #3887.
+                    //
+                    BufferedReader in = new BufferedReader(new InputStreamReader(i));
+                    String s;
+                    while((s = in.readLine()) != null) {
+                        responseBody.append(s);
+                        responseBody.append("\n");
+                    }
                 }
                 String errorHeader = null;
@@ -553,15 +561,13 @@
 
                 errorHeader = errorHeader == null? null : errorHeader.trim();
-                String errorBody = responseBody == null ? null : responseBody.toString().trim();
+                String errorBody = responseBody.length() == 0? null : responseBody.toString().trim();
                 switch(retCode) {
                 case HttpURLConnection.HTTP_OK:
-                    break; // do nothing
+                    return responseBody.toString();
                 case HttpURLConnection.HTTP_GONE:
                     throw new OsmApiPrimitiveGoneException(errorHeader, errorBody);
                 default:
                     throw new OsmApiException(retCode, errorHeader, errorBody);
-
-                }
-                return responseBody.toString();
+                }
             } catch (UnknownHostException e) {
                 throw new OsmTransferException(e);
