-1

I am trying to fetch data from php server on android using json parsing. here's my code of android.....

Am not able to fetch data and getting some error for which I've attached screenshot below.I am not able to figure it out.

package com.example.ecogreen;
import android.os.AsyncTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class FetchData extends AsyncTask {


    String fetched_data="",extracted="";

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        Features.data.setText(this.extracted);
    }

    @Override
    protected Object doInBackground(Object[] objects) {
        try {
            URL url = new URL("https://192.168.0.197/?");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while(line != null){
                line = bufferedReader.readLine();

            }


            JSONArray JA = new JSONArray();
            for(int i =0 ;i < JA.length(); i++){
                JSONObject JO = (JSONObject) JA.get(i);



                fetched_data = "Temperature" + JO.getString("temprature") + "\n" +
                        "moisture1" + JO.getString("moisture1") + "\n" +
                        "humidity" + JO.getString("humidity") + "\n" +
                        "moisture2" + JO.getString("moisture2") + "\n" ;


                extracted = extracted + fetched_data + "\n";
}

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

        return null;


    }
}

I put this data in array and encoded to json. This is how it shows:

i put this data in array and encoded to json..This is how it shows This is the error coming when am trying to fetch:

Attempt to invoke virtual method 'void android.widget.TextView.SetText(java.lang.CharSequence)' on a null object reference

this is the error coming when am trying to fetch

ChrisF
  • 134,786
  • 31
  • 255
  • 325

1 Answers1

0

Check if you are binding your TextView with findViewById() or whichever other method you are using to bind your views

Tom Munyiri
  • 156
  • 2
  • 5