Message from discussion
RestAssuredResponseImpl: getting body as String results in NPE
Received: by 10.216.235.32 with SMTP id t32mr618909weq.7.1348913845649;
Sat, 29 Sep 2012 03:17:25 -0700 (PDT)
X-BeenThere: rest-assured@googlegroups.com
Received: by 10.180.73.226 with SMTP id o2ls3193928wiv.0.gmail; Sat, 29 Sep
2012 03:17:25 -0700 (PDT)
Received: by 10.180.73.173 with SMTP id m13mr339790wiv.4.1348913845252;
Sat, 29 Sep 2012 03:17:25 -0700 (PDT)
Received: by 10.180.73.173 with SMTP id m13mr339789wiv.4.1348913845239;
Sat, 29 Sep 2012 03:17:25 -0700 (PDT)
Return-Path: <johan.hal...@gmail.com>
Received: from mail-we0-f177.google.com (mail-we0-f177.google.com [74.125.82.177])
by gmr-mx.google.com with ESMTPS id hm1si216274wib.3.2012.09.29.03.17.25
(version=TLSv1/SSLv3 cipher=OTHER);
Sat, 29 Sep 2012 03:17:25 -0700 (PDT)
Received-SPF: pass (google.com: domain of johan.hal...@gmail.com designates 74.125.82.177 as permitted sender) client-ip=74.125.82.177;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of johan.hal...@gmail.com designates 74.125.82.177 as permitted sender) smtp.mail=johan.hal...@gmail.com; dkim=pass header...@gmail.com
Received: by weyu50 with SMTP id u50so2319194wey.22
for <rest-assured@googlegroups.com>; Sat, 29 Sep 2012 03:17:25 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20120113;
h=mime-version:in-reply-to:references:date:message-id:subject:from:to
:content-type;
bh=KNiAMrWSqBf5A+xakgqlaz4glgETPEo0pU/IA7ZAMfk=;
b=PyzTZsZssuJv0FFwSXvqRchl2+kC1DZdmdKRwAvS7gz3OUTgvDKzYg6D59FHki+4BX
BSQBRkADqyZP53tvmKe7pMyoCSO0XAn3ZDNlF1INJPQIXz9K/gt42opdSEk/9aPKIeoY
n/i4P7fPuI3vp5iwlZIHaFA9QeuQY03SOwd8uFhJlW7Xl34Rznzp/W00dfT1EZZ+rjnq
r2mq0+bxSYXFCtvAD1XBgtSKSyr7RWNYpGJNDbAh6FLfBIIXftM785PQmB3//dHrBC2E
eQmyR+aE6Wr+LLLbq58xXv9S1NqELJTbRyBEwjZao+U+UlpEQQB7HpUQYtzHqkNBM8Su
lYJQ==
MIME-Version: 1.0
Received: by 10.216.150.197 with SMTP id z47mr4715175wej.221.1348913845147;
Sat, 29 Sep 2012 03:17:25 -0700 (PDT)
Received: by 10.216.193.218 with HTTP; Sat, 29 Sep 2012 03:17:25 -0700 (PDT)
In-Reply-To: <f55e69ea-8ba9-439f-a39d-60937f9a6...@t4g2000vba.googlegroups.com>
References: <f55e69ea-8ba9-439f-a39d-60937f9a6...@t4g2000vba.googlegroups.com>
Date: Sat, 29 Sep 2012 12:17:25 +0200
Message-ID: <CAK3tnc+1pAbJ9AFChQ1s_tNxU7Wgih+GD6aChtJo9vfzM7c...@mail.gmail.com>
Subject: Re: [rest-assured] RestAssuredResponseImpl: getting body as String
results in NPE
From: Johan Haleby <johan.hal...@gmail.com>
To: rest-assured@googlegroups.com
Content-Type: multipart/alternative; boundary=0016e6daa612929a2404cad47a62
--0016e6daa612929a2404cad47a62
Content-Type: text/plain; charset=ISO-8859-1
Hmm it sounds like a bug indeed but it's very hard to tell what's going on.
Can you provide some details on the data (body) your resource returns?
/Johan
On Fri, Sep 28, 2012 at 8:54 PM, bjello <devnull2...@gmx.de> wrote:
> Hi!
>
> I'm new to RestAssured (using 1.6.2). When I tried to read the content
> of the response's body as a String, I got a NullPointerException.
> However, during debugging the REST server logs successful messages and
> the content length of the response is as expected, the result is
> there, I just can not read it. Then I tried to read the body as
> InputStream and it worked. So, is there a problem with my code?
>
> Response response = RestAssured.get(path);
> String token = response.getBody().asString();
>
> I also tried to use print() or prettyPrint() but I got the same
> results.
> The exception gets thrown in RestAssuredResponseImpl in
>
> private String convertStreamToString(InputStream is) throws
> IOException {
> Writer writer = new StringWriter();
> char[] buffer = new char[1024];
> Reader reader;
> try {
> reader = new BufferedReader(new InputStreamReader(is,
> findCharset()));
> int n;
> while ((n = reader.read(buffer)) != -1) {
> writer.write(buffer, 0, n);
> }
> } finally {
> is.close();
> reader.close();
> }
> return writer.toString();
> }
>
> When I reach finally, the reader is already null again:
> java.lang.NullPointerException: Cannot invoke method close() on null
> object
> Any idea why I can't use asString or prettyPrint?
>
> Cheers!
> Bjello
>
--0016e6daa612929a2404cad47a62
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Hmm it sounds like a bug indeed but it's very hard to tell what's g=
oing on. Can you provide some details on the data (body) your resource retu=
rns?<div><br></div><div>/Johan<br><br><div class=3D"gmail_quote">On Fri, Se=
p 28, 2012 at 8:54 PM, bjello <span dir=3D"ltr"><<a href=3D"mailto:devnu=
ll2...@gmx.de" target=3D"_blank">devnull2...@gmx.de</a>></span> wrote:<b=
r>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">Hi!<br>
<br>
I'm new to RestAssured (using 1.6.2). When I tried to read the content<=
br>
of the response's body as a String, I got a NullPointerException.<br>
However, during debugging the REST server logs successful messages and<br>
the content length of the response is as expected, the result is<br>
there, I just can not read it. Then I tried to read the body as<br>
InputStream and it worked. So, is there a problem with my code?<br>
<br>
Response response =3D RestAssured.get(path);<br>
String token =3D response.getBody().asString();<br>
<br>
I also tried to use print() or prettyPrint() but I got the same<br>
results.<br>
The exception gets thrown in RestAssuredResponseImpl in<br>
<br>
private String convertStreamToString(InputStream is) throws<br>
IOException {<br>
=A0 =A0 Writer writer =3D new StringWriter();<br>
=A0 =A0 char[] buffer =3D new char[1024];<br>
=A0 =A0 Reader reader;<br>
=A0 =A0 try {<br>
=A0 =A0 =A0 =A0 reader =3D new BufferedReader(new InputStreamReader(is,<br>
findCharset()));<br>
=A0 =A0 =A0 =A0 int n;<br>
=A0 =A0 =A0 =A0 while ((n =3D reader.read(buffer)) !=3D -1) {<br>
=A0 =A0 =A0 =A0 =A0 =A0 writer.write(buffer, 0, n);<br>
=A0 =A0 =A0 =A0 }<br>
=A0 =A0 } finally {<br>
=A0 =A0 =A0 =A0 is.close();<br>
=A0 =A0 =A0 =A0 reader.close();<br>
=A0 =A0 }<br>
=A0 =A0 return writer.toString();<br>
}<br>
<br>
When I reach finally, the reader is already null again:<br>
java.lang.NullPointerException: Cannot invoke method close() on null<br>
object<br>
Any idea why I can't use asString or prettyPrint?<br>
<br>
Cheers!<br>
<span class=3D"HOEnZb"><font color=3D"#888888">Bjello<br>
</font></span></blockquote></div><br></div>
--0016e6daa612929a2404cad47a62--