-1

Java File:

I want to update existing row in MySQl database through android app. i am getting null pointer exception. below is my code along with logcat. i have tried to fix it but not got successfull. kindly help. web service works well when run using HTML. but not working when connected with android.

 private ProgressDialog pDialog;

        JSONParser jsonParser = new JSONParser();
        int success=0;
        double pickup_lat, pickup_lng, drop_lat, drop_lng;
        // url to create new product
        private static String url_create_product = "http://192.168.10.6:8080/courier/add_location.php";

        // JSON Node names
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_USERNO = "user_no";
        private static final String TAG_PICKUP_LAT = "pickup_lat";
        private static final String TAG_PICKUP_LNG = "pickup_lng";
        private static final String TAG_DROP_LAT = "drop_lat";
        private static final String TAG_DROP_LNG = "drop_lng";

     class AddLocation extends AsyncTask<String, String, String> {

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

            /**
             * Creating product
             */
            protected String doInBackground(String... args) {

                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
               params.add(new BasicNameValuePair(TAG_USERNO, "120"));
                params.add(new BasicNameValuePair(TAG_PICKUP_LAT, "12"));
                params.add(new BasicNameValuePair(TAG_PICKUP_LNG,  "12"));
                params.add(new BasicNameValuePair(TAG_DROP_LAT,  "12"));
                params.add(new BasicNameValuePair(TAG_DROP_LNG, "12"));


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

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

                } 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();
                if (success == 1) {
                    // successfully created product
                    Intent i = getIntent();
                    // send result code 100 to notify about product update
                    setResult(100, i);
                    finish();
                    // closing this screen

                    finish();
                } else {

                    //failed to create product
                }
            }
        }
        @Override
        public void onDestroy(){
            super.onDestroy();

            pDialog.dismiss();
            pDialog.cancel();
        }
    }

Web service code:

<?php header('Access-Control-Allow-Origin: *'); ?>
<?php header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept'); ?>
<?php header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT'); ?>
<?php
// array for JSON response
$response = array();
if (isset($_POST['user_no']) && isset($_POST['pickup_lat']) && isset($_POST['pickup_lng']) && isset($_POST['drop_lat']) && isset($_POST['drop_lng'])) {
   $user_no = $_POST['user_no'];
    $pickup_lat = $_POST['pickup_lat'];
    $pickup_lng = $_POST['pickup_lng'];
    $drop_lat= $_POST['drop_lat'];
    $drop_lng = $_POST['drop_lng'];
     }

$db_host = "localhost"; 
// Place the username for the MySQL database here 
$db_username = "root"; 
// Place the password for the MySQL database here 
$db_pass = "";  
// Place the name for the MySQL database here 
$db_name = "smartcourier";

// Run the actual connection here  
$link=mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysqli_select_db($link,"$db_name") or die ("no database");

$sql = "UPDATE courier_detail SET pickup_lat='$pickup_lat' , pickup_lng='$pickup_lng',  drop_lat= '$drop_lat', drop_lng=$drop_lng WHERE user_no=$user_no";
$result =mysqli_query($link,$sql);

if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Location added successfully.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);
    }

@mysql_close($link);

?>

log cat

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
  • i have checked those links but not getting anything useful in my case. therefore i have asked separately – asifa mahmood Mar 17 '18 at 05:22
  • 1
    http://idownvotedbecau.se/nodebugging/, e.g. examining the value of `json`, realizing it is null and the [duplicate link](https://stackoverflow.com/q/218384/5221149) is *right on point*, then look at and/or debug code inside `makeHttpRequest` to see why it returns `null`, a job that we can't do since you haven't shared that code, and it's not built-in Android. – Andreas Mar 17 '18 at 05:43

1 Answers1

0

This is probably because jsonParser.makeHttpRequest() method return null object and that is assigned to json object. That's why you get null pointer exception when trying to getInt() from json object. Please check your jsonParser.makeHttpRequest() again or check if jsonParser.makeHttpRequest() return not null object by this.

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

            // check if json is null or not
            Log.i("Json Check : ", json == null + "");

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

            } catch (JSONException e) {
                e.printStackTrace();

            }
Aung Si Min Htet
  • 1,214
  • 3
  • 10
  • 12