有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么在println时即使有值显示也会出现空指针异常?

这是类a的一个函数,我将我的值传递给一个类名,并确认它的寄存器函数

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

这是课堂确认。在这门课上,我计划在服务器内部发布数据。url中没有错误。我已经打印了这个代码“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) 个答案

  1. # 1 楼答案

    读取代码时,如果调用confirm.register(id),则会出现空指针异常

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

    public final class confirm {...}
    

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

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

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

    如果它不能解决你的错误,请在这里填写错误信息

  2. # 2 楼答案

    您有一个实例变量,它从未分配给:

    ProgressDialog loading;
    

    它被用于

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