-1

I am very new in android.In my app I want to set retrieved data from mysql database in different textview.I tried many way but I am unable to do it.Always get a error.I also tried that.Please help me.I Want to set that data in textview.

I call this class from onCreate method

/*ERROR LINE*/private class GetData extends AsyncTask<String, String, String> {
    public Context context;

    public GetData(Context context) {
    }

    @Override
    protected String doInBackground(String... params) {
        HttpClient myClient = new DefaultHttpClient();
        HttpPost myConnection = new HttpPost("http://www.mybusket.com/webapi/carrental/car/get_car_details.php?vcarid="+Id); //this Id is a int variable

        try {
            response = myClient.execute(myConnection);
            str = EntityUtils.toString(response.getEntity(), "UTF-8");
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        }
            catch (IOException e) {
            e.printStackTrace();
        }
        try {
            JSONObject obj = new JSONObject(str);
            JSONArray jsonArray = obj.getJSONArray("car");

                JSONObject json=jsonArray.getJSONObject(0);
                brand.setText(json.getString("brand"));//ERROR LINE
                fuel.setText(json.getString("fueltype"));
                carno.setText(json.getString("carno"));
                seat.setText(json.getString("seat"));
                feature.setText(json.getString("feature"));

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

        return null;
    }

}

ERROR:E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.arpan.carrental, PID: 15029 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.arpan.carrental.CarView$1.onResponse(CarView.java:130) at com.example.arpan.carrental.CarView$1.onResponse(CarView.java:115)

This is my php

  <?php
  $response = array();

  // include db connect class

  require_once("../connectdb.php");
  // connecting to db
  $db = new DB_CONNECT();
  // check for post data
 if (isset($_GET["carid"])) {
 $carid = $_GET['carid'];
 // check for post data

 if (isset($_GET["carid"])) {
 $carid = $_GET['carid'];
 $result = mysql_query("SELECT * FROM carinfo WHERE carid = $carid");

if (!empty($result)) {
    // check for empty result
    if (mysql_num_rows($result) > 0) {

        $row = mysql_fetch_array($result);

        $car = array();
        $car["brand"] = $row["brand"]; 
        $car["fueltype"] = $row["fueltype"]; 
        $car["carno"] = $row["carno"]; 
        $car["seat"] = $row["seat"]; 
        $car["feature"] = $row["feature"];      
        // success
        $response["success"] = 1;

        // user node
        $response["car"] = array();

        array_push($response["car"], $car);

        // echoing JSON response
        echo json_encode($response);
    } else {
        // no employer found
        $response["success"] = 0;
        $response["message"] = "No Car found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // no employer found
    $response["success"] = 0;
    $response["message"] = "No Car found";

    // echo no users JSON
    echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>

I want all data comes from database and display in different textview.

Arpan Sarkar
  • 189
  • 3
  • 14

2 Answers2

0

You may not initilized your text view before using it.

brand = findViewById(R.id.textView);

and also check your text whether it is null or not before setting it into textview.

If you are dealing with json you can test this as

if(json.has("brand")){
           //brand fild is availble in json
          //set it to brand textview 
        }
Nouman Ch
  • 4,023
  • 4
  • 29
  • 42
0

You Change the UI in background thread, may that is the reason of issue . , Apply this code . Assume you already initialize the Textview

 @Override
        protected String doInBackground(String... params) {
            HttpClient myClient = new DefaultHttpClient();
            HttpPost myConnection = new HttpPost("url");  

            try {
                response = myClient.execute(myConnection);
                str = EntityUtils.toString(response.getEntity(), "UTF-8");
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            }
                catch (IOException e) {
                e.printStackTrace();
            }

            return str ;
        }


      @Override
       protected void onPostExecute(String result) {
              if(str!=null)
              {
                try {
                JSONObject obj = new JSONObject(str);
                JSONArray jsonArray = obj.getJSONArray("car");
                if(jsonArray.length()>0)
                  {
                    JSONObject json=jsonArray.getJSONObject(0);
                    brand.setText(json.getString("brand"));
                    fuel.setText(json.getString("fueltype"));
                    carno.setText(json.getString("carno"));
                    seat.setText(json.getString("seat"));
                    feature.setText(json.getString("feature"));
    }
            } catch (JSONException e) {
                e.printStackTrace();
            }

}
                }
Praveen Rawat
  • 314
  • 1
  • 3
  • 14