2016-05-29 71 views
0

當我從Android應用程序添加數據到MySQL數據庫時,我得到NullPointerException,所以當我點擊創建新產品時,會發生此異常。這是我的代碼在我的數據庫中添加新產品:NullPointer異常同時將數據從Android添加到MySQL數據庫

public class NewProductActivity extends Activity { 

    // Progress Dialog 
    private ProgressDialog pDialog; 

    JSONParser jsonParser = new JSONParser(); 
    EditText inputName; 
    EditText inputPrice; 
    EditText inputDesc; 

    // url to create new product 
    private static String url_create_product = "http://192.168.56.1/products/create_product.php"; 

    // JSON Node names 
    private static final String TAG_SUCCESS = "success"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.add_product); 

     // Edit Text 
     inputName = (EditText) findViewById(R.id.inputName); 
     inputPrice = (EditText) findViewById(R.id.inputPrice); 
     inputDesc = (EditText) findViewById(R.id.inputDesc); 

     // Create button 
     Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct); 

     // button click event 
     btnCreateProduct.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 

       String name = inputName.getText().toString(); 
       String price = inputPrice.getText().toString(); 
       String description = inputDesc.getText().toString(); 
       // creating new product in background thread 
       new CreateNewProduct().execute(name,price,description); 
      } 
     }); 
    } 

    /** 
    * Background Async Task to Create new product 
    * */ 
    class CreateNewProduct extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(NewProductActivity.this); 
      pDialog.setMessage("Creating Product.."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     /** 
     * Creating product 
     * */ 
     protected String doInBackground(String... args) { 
      String name = args[0], 
        price = args[1], 
        description = args[2]; 
      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("name", name)); 
      params.add(new BasicNameValuePair("price", price)); 
      params.add(new BasicNameValuePair("description", description)); 

      // getting JSON Object 
      // Note that create product url accepts POST method 
    **line 102**  JSONObject json = jsonParser.makeHttpRequest(url_create_product, 
        "POST", params); 

      // check log cat fro response 
      Log.d("Create Response", json.toString()); 

      // check for success tag 
      try { 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 
        // successfully created product 
        Intent i = new Intent(getApplicationContext(), AllProductsActivity.class); 
        startActivity(i); 

        // closing this screen 
        finish(); 
       } else { 
        // failed to create product 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog once done 
      pDialog.dismiss(); 
     } 

    } 
} 

我檢查了我的PHP代碼,它工作正常:

<?php 
    $con = mysql_connect('localhost', 'root',''); 
    mysql_select_db('database',$con); 

    $name = $_POST['name']; 
    $price = $_POST['price']; 
    $description = $_POST['description']; 
    $sql = "insert into products(name, price, description)     
      values('$name','$price','$description')"; 
    mysql_query($sql); 
?> 

的logcat:

FATAL EXCEPTION: AsyncTask #1 
java.lang.RuntimeException: An error occured while executing doInBackground() 
    at android.os.AsyncTask$3.done(AsyncTask.java:299) 
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 
    at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:239) 
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
    at java.lang.Thread.run(Thread.java:856) 
aused by: java.lang.NullPointerException 
    at com.example.hanen.test1.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:102) 
    at com.example.hanen.test1.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:68) 
    at android.os.AsyncTask$2.call(AsyncTask.java:287) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)  
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)  
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)  
    at java.lang.Thread.run(Thread.java:856)  
+0

你知道你的代碼哪一行導致異常嗎? – pooyan

+0

NewProductActivity:102 –

+0

你可以給這個類的所有東西包括import語句嗎?例外是指向第102行,你可以檢查你有什麼102行,可能會導致NullPointerException? – LeTex

回答

0

我不有問題的答案,因爲你沒有在你的列表中包括行號,我不打算手動編號。不過,查找並修復這種類型的錯誤是死簡單。以下是具體步驟:

  1. 打開行號在你的IDE
  2. 查找對應於最上面的線是指在你的代碼行的堆棧跟蹤你的代碼行。在這種情況下,文件NewProductActivity.java中的第102行
  3. 在該行上,將會有一個表達式,類似於variable.something
  4. 當該行執行時variable爲空。找出爲什麼。
+0

感謝您響應此行是JSONObject json = jsonParser.makeHttpRequest(url_create_product, 「POST」,params);我認爲問題是關係到這一行private static String url_create_product =「http://192.168.56.1/products/create_product.php」;但我檢查出iPt是v4是192.168.56.1,我不知道如何解決這個問題 – honeyyy

+0

打賭你錯了。打賭你的問題是,這是jsonParser爲空。 –

+0

它是空的,因爲在調試時,客戶端還沒有準備好。 連接到目標VM,地址:'localhost:8626',transport:'socket' – honeyyy