gpt4 book ai didi

java - 为什么 println 时有值显示却出现空指针异常?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:15:26 30 4
gpt4 key购买 nike

这是 A 类的一个函数,我将我的值传递给类名以确认其注册函数。

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

这是类(class)确认。在本类(class)中,我计划在服务器内发布数据。 url中没有错误。我有 println 这段代码“System.out.println("wei"+ getPostDataString(postDataParams));"我已经得到了我的值(value)。但是系统说空指针。其他部分代码有没有错误?

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

最佳答案

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

ProgressDialog loading;

它用于

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

关于java - 为什么 println 时有值显示却出现空指针异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36739823/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com