为什么我得到空指针异常,即使println有值显示?

时间:2016-04-20 09:42:33

标签: java android

这是A类的一个函数,我将我的值传递给类名确认为其寄存器函数。

private void registerOrder() {
    confirm.register(id);
}

这是课堂确认。在这个课程中,我计划在服务器内发布数据。网址没有错误。我打印过这段代码" System.out.println(" wei" + getPostDataString(postDataParams));"我已经获得了我的价值。但系统说空指针。其他代码中是否有任何错误?

 public class confirm {
 private static final String REGISTER_URL = 
 "http://192.168.43.214/apexStore2/confirm.php";

 public static void register(String id) {
    class RegisterUser extends AsyncTask<String, Void, String> {
        ProgressDialog loading;
 @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
        }
  @Override
        protected String doInBackground(String... params) {

            HashMap<String, String> data = new HashMap<>();
            data.put("product_id",params[0]);

            String result = sendPost(REGISTER_URL,data);

            return  result;
        }
    }

    RegisterUser ru = new RegisterUser();
    ru.execute(String.valueOf(id));
 }
 public static String sendPost(String requestURL,
                              HashMap<String, String> postDataParams) {

    URL url;
    String response = " ";
    try {
        url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", "" +                       
  Integer.toString(getPostDataString(postDataParams).getBytes().length));
        conn.setRequestProperty("Content-Language", "en-US");
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setDoInput(true);
        conn.setDoOutput(true);


        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));
        System.out.println("wei" + getPostDataString(postDataParams));

        writer.flush();
        writer.close();
        os.close();
        int responseCode=conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            BufferedReader br=new BufferedReader(new 
   InputStreamReader(conn.getInputStream()));
            response = br.readLine();
        }
        else {
            response="Error Registering";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return response;
}

private static String getPostDataString(HashMap<String, String> params) 
throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for(Map.Entry<String, String> entry : params.entrySet()){
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}
}

2 个答案:

答案 0 :(得分:2)

您有一个实例变量,它永远不会分配给:

ProgressDialog loading;

,用于

protected void onPostExecute(String s) {
    super.onPostExecute(s);
    loading.dismiss();     // expect NPE
}

答案 1 :(得分:0)

阅读你的代码当你调用confirm.register(id)时,我会出现空指针异常。

如果要以静态方式使用类,则需要将其声明为最终类:

public final class confirm {...}

对你来说应该有用。不过,您也可以通过创建类的实例来解决您的问题,而无需使用最终标识符:

confirm conf = new confirm();
conf.register(id);

我也看到你的班级名字以小写字母开头。建议创建以大写字母开头的类名。

如果不能解决您的错误,请在此处覆盖错误信息。