2013-10-29 41 views
0

我正在處理我的Web服務,並且遇到了一個奇怪的問題。我無法解決這個問題,因爲我是android開發新手。我在異步任務的onPostExecute()方法中收到空指針異常。當我將結果與字符串進行比較時發生這種情況。AsyncTask中的onPostExecute中的NullPointer異常

if (result.contains("Duplicate"))   or 
if (result.equals("Duplicate"))    or 
if (result.equalsIgnoreCase("Duplicate")) or 

所有三個給出錯誤。我通過System.out.println(result)打印了結果的值;它顯示null

爲了讓我的問題更加怪異,這種情況只有在我的老闆測試時纔會發生。其他時候它的功能非常好。設備和網絡都保持不變。此前它曾經只發生在仿真器上。我曾經重新啓動eclipse,並且問題用於解決問題。

請幫我一把!

代碼

private void startDownload() { 

    new AddTask().execute(FILENAME); 
} 

public class AddTask extends AsyncTask<String, Integer, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     dialog = ProgressDialog.show(ContactDetails.this, "Loading", 
       "Please Wait..."); 

    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 


      if (result.contains("Duplicate")) { //---- here error comes 

       int ecolor = -16777216; // whatever color you want 
       String estring = "Account with this email already exists!!"; 
       ForegroundColorSpan fgcspan = new ForegroundColorSpan(
         ecolor); 
       SpannableStringBuilder ssbuilder = new SpannableStringBuilder(
         estring); 
       ssbuilder.setSpan(fgcspan, 0, estring.length(), 0); 
       et2.setError(ssbuilder); 
       dialog.dismiss(); 

      } else { 
       Toast.makeText(ContactDetails.this, "Account Created!", 
         Toast.LENGTH_LONG).show(); 
       dialog.dismiss(); 
       myDialog = new Dialog(ContactDetails.this); 
       myDialog.setContentView(R.layout.email_alert); 
       myDialog.setTitle("Confirmation"); 
       myDialog.setCancelable(true); 

       // for OK 
       Button ok = (Button) myDialog.findViewById(R.id.alertOk); 
       ok.setOnClickListener(new OnClickListener() { 
        public void onClick(View v) { 

         Intent intent = new Intent(ContactDetails.this, 
           Login.class); 
         startActivity(intent); 
        } 
       }); 
       myDialog.show(); 
      } 
     } 

    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     // TODO Auto-generated method stub 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected String doInBackground(String... params) { 

     is = null; 
      individual(); 


     return is; 
    } 


    public void individual() { 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(
       "http://website.com/contact.php"); 
     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
        10); 
      nameValuePairs.add(new BasicNameValuePair("Name", str1)); 
      nameValuePairs.add(new BasicNameValuePair("Email", str2)); 
      nameValuePairs.add(new BasicNameValuePair("Password", str3)); 
      nameValuePairs.add(new BasicNameValuePair("Address", str5)); 
      nameValuePairs.add(new BasicNameValuePair("Pin", str11)); 
      nameValuePairs.add(new BasicNameValuePair("City", spt1)); 
      nameValuePairs.add(new BasicNameValuePair("State", str12)); 
      nameValuePairs.add(new BasicNameValuePair("phone_visibility", 
        str_radio)); 
      nameValuePairs.add(new BasicNameValuePair("No", str7)); 

      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      // httpclient.execute(httppost); 

      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = EntityUtils.toString(entity); 

      JSONArray jArray = new JSONArray(is); 

      for (int i = 0; i < jArray.length(); i++) { 

       JSONObject jObject = jArray.getJSONObject(i); 

      } 

     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

---編輯---

什麼可以得到一個空值作爲結果的原因是什麼?這是網絡問題嗎? 有沒有一種方法可以確保我不會得到空結果? 我的應用程序將無法正常工作,如果我得到空

+0

你爲什麼不檢查NPE? –

+1

是的...驗證結果字符串在onPostExecute –

回答

1

添加一張支票NPE的結果

@Override 
protected void onPostExecute(String result) 
{ 
    if(result == null) 
    { 
     // handle null result, debug or whatever 
     return; 
    } 
    .... 
    //rest of your code 
} 

編輯 我相信這裏是你的問題

HttpResponse response = httpclient.execute(httppost); 
HttpEntity entity = response.getEntity(); 
is = EntityUtils.toString(entity); 

響應可能變得無效值,讓response.getEntity();會引發異常,並且is = EntityUtils.toString(entity);將會被觸發,因此它將會有空值 ,並且在你doInBackground中返回is,它是空的:)所以你需要處理這個空值並且接受有時你會得到空值的事實。歡呼聲

1

檢查resultnull或not..if不那麼只有成效低於operations..like ..

if(result!=null){ 
if (result.contains("Duplicate"))   or 
if (result.equals("Duplicate"))    or 
if (result.equalsIgnoreCase("Duplicate")) or 
} 
+0

是的,但爲什麼結果爲空?所需的記錄存在於數據庫中。也在PHP我呼應的消息 – lemons

+0

因爲你有調試一次檢查所有的情況下.... –

1

你引起了很多例外情況在individual()。當其中一個發生時,您的result可能仍然爲空,但您不會知道這一點。

您應該:

  1. 更改異常捕獲代碼和存儲更多的信息,一個例外發生,並使用該信息onPostExecute

  2. 只要接受result可以爲空,並在onPostExecute(檢查null並執行一些適當的操作)中正確處理它。

+0

是的,但爲什麼結果爲空?所需的記錄存在於數據庫中。也在PHP我回應消息 – lemons

+0

它可以是任何例外,你打電話給服務器,任何事情都可能發生。你真的嘗試過調試,看看它是否進入任何'catch'塊嗎? – Szymon

+0

不,我通過放置敬酒和system.out語句來測試。我仍然必須學會正確調試 – lemons

2

檢查您是否正在從結果中獲取數據...

if(result!=null){ 
// here check the things you need 


} 

它爲我工作

0
protected String doInBackground 

回報字符串

您正在返回

onPostExecute(String result) 

流程,字符串

你不能撥打「包含」

修復:

@Override 

    protected String doInBackground(String... params) { 

    is = individual(); 

    return is; 
} 

public String individual() { 

...把​​你的JSON數據爲String ...