Java库兼容性问题 - NULL指针异常

时间:2012-06-26 20:50:00

标签: java android eclipse java-ee netbeans

我正在尝试创建一个Java swing应用程序,我正在尝试使用我为Android编写的类。我在执行http帖子时遇到问题,因为我收到了NULL指针异常。

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());
}

堆栈追踪:

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)
    ...

在android上,org.apache。*包包含在android-8.jar中。它只是不能使用我从here下载的库(HttpCore 4.2.1)。

在以下调用new UrlEncodedFormEntity(constraints)上抛出NullPointerException。我已经使用调试器检查了参数constraints,它非常好并且包含它应该包含的内容。

我做错了吗?在我看来,lib存在问题。我一直在the site寻找以前的版本,但似乎没有。

编辑:我刚刚发现下载源代码以确切了解异常的位置(第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:  }

2 个答案:

答案 0 :(得分:1)

你什么时候下载图书馆的?你看过this post了吗?建议是在后来的构建中解决这个问题。

鉴于您的编辑,我会说您拥有的二进制文件和您下载的源代码不同步。我建议重新下载二进制文件并尝试使用这些二进制文件运行。

答案 1 :(得分:1)

似乎在HttpClient 4.2.1的org.apache.http.entity.StringEntity类中破坏了StringEntity。问题似乎涉及Charset。补丁可在 http://dvdsdirect.us/notesstringentity.jsp