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.
private static String httpPost(ArrayList<NameValuePair> 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());
}
Stack trace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.apache.http.entity.StringEntity.<init>(StringEntity.java:70)
at org.apache.http.client.entity.UrlEncodedFormEntity.<init>(UrlEncodedFormEntity.java:78)
at org.apache.http.client.entity.UrlEncodedFormEntity.<init>(UrlEncodedFormEntity.java:92)
at connection.Server.httpPost(Server.java:130)
...
On android the org.apache.* package is included in android-8.jar. It just wont work with the libs i’ve downloaded from here (HttpCore 4.2.1).
The NullPointerException is thrown on the following call new UrlEncodedFormEntity(constraints). I’ve checked the parameter constraints using the debugger and it’s perfectly fine and contains what it’s supposed to contain.
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 the site but it seems that there are none there.
EDIT: It just occurred to me to download the sources to see exactly where the exception is beeing thrown (line 70):
org.apache.http.entity.StringEntity
68: Charset charset = contentType != null ? contentType.getCharset() : null;
69: if (charset == null) {
70: charset = HTTP.DEF_CONTENT_CHARSET;
80: }
When did you download the library? Have you seen this post? The suggestion is that it was a regression that has been addressed in a later build.
Given your edit, I would say the binaries you have and the source you downloaded are out of sync. I would suggest re-downloading the binaries and attempting to run your application with those.