2016-04-20 73 views
4

這是類A的功能,我將我的值傳遞給類名確認其註冊函數。即使println有值顯示,爲什麼我會得到空指針異常?

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

這是等級確認。在這個課上,我有計劃在服務器內發佈數據。網址沒有錯誤。我有println這個代碼「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(); 
} 
} 
+0

的println需要消息中使用時,發生當沒有數據在登錄陳述或顯示SOP的。附上一個調試器,並檢查你的價值是否得到。 –

+0

看看從NPE的堆棧跟蹤,看看它從哪裏拋出。 – gmaslowski

+0

進行調試,我應該這樣寫嗎? Log.d(getPostDataString(postDataParams),「onCreate()」); –

回答

0

讀你的代碼我supose空指針異常,當你調用confirm.register(id)出現。

如果你想使用一個類以靜態方式,你需要將其聲明爲final類:

public final class confirm {...} 

對你來說應該工作。不過你也可以解決你的問題,而通過創建類的實例使用最終標識符:

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

我也看到你的類名稱以小寫字母開頭。建議用大寫字母開始創建類名。

如果它不能解決您的錯誤,請在此處列出錯誤信息。

+0

空指針是通過消除load.dismiss();從onpostexecute。但是數據不會傳遞到數據庫。我的方法有問題嗎? –

2

你有一個實例變量,這是從未分配給:

ProgressDialog loading; 

並且在

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

當我調試時,我得到的值 –

+0

你的意思是哪個值? – laune

+0

這一個System.out.println(「wei」+ getPostDataString(postDataParams)); –

相關問題