Note that there are some explanatory texts on larger screens.

plurals
  1. POJava library compatibility issue - NULL Pointer exception
    text
    copied!<p>I'm trying to create a Java swing application and I'm trying to use a class I wrote for Android. I'm having trouble executing a http post because I'm getting a NULL pointer exception.</p> <pre><code>private static String httpPost(ArrayList&lt;NameValuePair&gt; constraints, URI uri) throws ClientProtocolException, IOException { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(uri); try { httppost.setEntity(new UrlEncodedFormEntity(constraints)); } catch (UnsupportedEncodingException e) { throw (new IllegalArgumentException("Constraints: " + constraints.toString())); } HttpResponse response = httpclient.execute(httppost); return isToString(response.getEntity().getContent()); } </code></pre> <p>Stack trace: </p> <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at org.apache.http.entity.StringEntity.&lt;init&gt;(StringEntity.java:70) at org.apache.http.client.entity.UrlEncodedFormEntity.&lt;init&gt;(UrlEncodedFormEntity.java:78) at org.apache.http.client.entity.UrlEncodedFormEntity.&lt;init&gt;(UrlEncodedFormEntity.java:92) at connection.Server.httpPost(Server.java:130) ... </code></pre> <p>On android the org.apache.* package is included in android-8.jar. It just wont work with the libs i've downloaded from <a href="http://hc.apache.org/downloads.cgi" rel="nofollow">here</a> (HttpCore 4.2.1). </p> <p>The NullPointerException is thrown on the following call <code>new UrlEncodedFormEntity(constraints)</code>. I've checked the parameter <code>constraints</code> using the debugger and it's perfectly fine and contains what it's supposed to contain.</p> <p>Am i doing something wrong? It seems to me that there is a problem with the lib. I've been looking for previous versions on <a href="http://hc.apache.org/downloads.cgi" rel="nofollow">the site</a> but it seems that there are none there.</p> <p><strong>EDIT:</strong> It just occurred to me to download the sources to see exactly where the exception is beeing thrown (line 70):</p> <p>org.apache.http.entity.StringEntity</p> <pre><code>68: Charset charset = contentType != null ? contentType.getCharset() : null; 69: if (charset == null) { 70: charset = HTTP.DEF_CONTENT_CHARSET; 80: } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload