Search code examples
javaandroidandroid-studioandroid-layoutandroid-asynctask

Async Task Attemp to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)'


Im Getting invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at app.VOTD_Data.onPostExecute.

Async Task

public class VOTD_Data extends AsyncTask<Void, Void, Void> {

    private String verseData = "";
    private String dailyverse = "";
    private String verseauthor = "";
    private String dailVersePref = "";
    private String verseAuthorPref = "";
    private SharedPreferences sharedPreferences;

    private Context context;

    public VOTD_Data(Context context){
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        dailVersePref = sharedPreferences.getString("dailyverse", "");
        verseAuthorPref = sharedPreferences.getString("verseauthor", "");
    }

    public VOTD_Data() {

    }


    @Override
    protected Void doInBackground(Void... voids) {

        try {
            URL url = new URL("https://beta.ourmanna.com/api/v1/get/?format=json");

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            String line = "";

            while (line != null){

                line = bufferedReader.readLine();
                verseData  = verseData + line;
            }

            JSONObject mainObject = new JSONObject(verseData).getJSONObject("verse");
            JSONObject verseObject = mainObject.getJSONObject("details");

            dailyverse = verseObject.getString("text");
            verseauthor = verseObject.getString("reference");

            sharedPreferences
                    .edit()
                    .putString("dailyverse", dailyverse)
                    .putString("verseauthor", verseauthor)
                    .apply();


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

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        //Daily Verse Activity
        DailyVerse_Activity.data.setText(dailyverse.toString());

    }
}

Main Activity

public class DailyVerse_Activity extends AppCompatActivity {


    public static TextView data;

    private ImageView banner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_daily_verse);

        data = (TextView) findViewById(R.id.dataText);

        VOTD_Data process = new VOTD_Data(DailyVerse_Activity.this);
        process.execute();


    }

    //On Back Pressed
    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        finish();
        return true;
    }

}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DailyVerse_Activity">

    <TextView
        android:id="@+id/dataText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:gravity="center"
        android:hint="Verse Data"
        />

</RelativeLayout>

Im Getting invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at app.VOTD_Data.onPostExecute.app.VOTD_Data.onPostExecute(VOTD_Data.java:88) at app.VOTD_Data.onPostExecute(VOTD_Data.java:18)


Solution

  • Pass your rootView to AsyncTask and get it's reference from there. Add one more parameter in your VOTD_Data class like this.

    public VOTD_Data(Context context, TextView textView)
    

    In postExecute just do:

    textView.setText(dailyverse.toString());
    

    And in your Activity class while calling the AsyncTask pass the textView to VOTD_Data class constructor like this:

    VOTD_Data process = new VOTD_Data(DailyVerse_Activity.this, data);
    process.execute();