-4

I'm trying to connect my android app to PHP page.. this is AsyncTask class function:

@Override
    protected String doInBackground(String... params) {
        try {
            accountDetails.add(new BasicNameValuePair("Name",params[0]));

            JSONObject json = jparser.makeHttpRequest("http://site.page.php","POST",accountDetails);

           /*My Problem is here..*/
           String registerReport = json.getString("registerReport");
        } catch (Exception e) { 
            error = e.toString();
        }

        return null;
    }

and my php page return this:

{"registerReport":"1"}

And then i got this error: java.lang.NullPointerException: Attemp to invoke virtual method 'java.lang.Stringorg.json.JSONObject.getString(java.lang.String)' on a null object reference

hassan
  • 7,013
  • 2
  • 20
  • 32
Zahi Ram
  • 5
  • 5

1 Answers1

-1

Your error

java.lang.NullPointerException: Attemp to invoke virtual method 'java.lang.Stringorg.json.JSONObject.getString(java.lang.String)' on a null object reference

means that you are trying to call the getString() method on a null object.

In your specific case, your object called 'json' is null after this initialization :

JSONObject json = jparser.makeHttpRequest("http://site.page.php","POST",accountDetails);

To fix this error, you can try if your object is null before to call the getString() method :

@Override
    protected String doInBackground(String... params) {
        try {
            accountDetails.add(new BasicNameValuePair("Name",params[0]));

            JSONObject json = jparser.makeHttpRequest("http://site.page.php","POST",accountDetails);

           if (json == null) {
               return null;
           }
           String registerReport = json.getString("registerReport");
        } catch (Exception e) { 
            error = e.toString();
        }

    return null;
}
Thomas Mary
  • 1,216
  • 1
  • 11
  • 22
  • The Error gone but still i can't get the value of the registerReport, do you have any idea how to fix this? :( – Zahi Ram Feb 11 '18 at 12:07
  • because `jparser.makeHttpRequest("http://site.page.php","POST",accountDetails); ` returns a null object. Please check (in a browser for example) that your server (http://site.page.php) can respond to a POST method. – Thomas Mary Feb 11 '18 at 12:11
  • `$response = array(); $response["registerReport"] = "1"; echo json_encode($response); ` .. I'm Sure that the page is written property. – Zahi Ram Feb 11 '18 at 12:16