Skip to content Skip to sidebar Skip to footer

How Can I Fix 'void Android.widget.textview.settext(java.lang.charsequence)' On A Null Object Reference?

I'm new to android. I am currently developing an app that displays multiple locations on a map which has its marker locations taken from a central database that I have already set

Solution 1:

As mentioned in the comments, you need to implement a callback using an interface to use the results of an Async Task. First create an interface.

New -> Jave Class -> Interface -> CallbackInterface .java. Define the method prototypes.

CallbackInterface.java

publicinterfaceCallbackInterface {
    publicvoidonSuccess(String callbackData);
    publicvoidonFailure();
}

In your MainActivity, define a class which implements the interface. Note that, the methods must be defined here and must contain the code to make use of the result of your Async Task MainActivity.java

publicclassMainActivityextendsAppCompatActivity {
    TextView data;
    Button performAsync;
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Add this without fail.
        data = findViewById(R.id.dataTV);

        performAsync = findViewById(R.id.performAsync);

        performAsync.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {


       //Recreating your "process"newAsyncTaskClass(newCallbackClass()).execute();
        }
    });

}

/*Create a class which implements the interface. It is a good practise to have
onSuccess and onFailure methods */publicclassCallbackClassimplementsCallbackInterface {

    @OverridepublicvoidonSuccess(String callbackData) {
        //do your callback functions here. In your case, setting the textview to result of parsed data.
        data.setText(callbackData);
    }

    @OverridepublicvoidonFailure() {

    }
}

}

Finally, in your fetchData class, (AsyncTaskClass in my code) , do your asynchronous operations and finally in postExecute, call the onSuccess method with appropriate parameters. Note that you should pass the class as a parameter to the fetchData class and using the constructor, you must assign it to the instance of the interface. AsyncTaskClass.java

publicclassAsyncTaskClassextendsAsyncTask {
    CallbackInterface callbackInterface;
    String dataParsed;
    publicAsyncTaskClass(CallbackInterface callbackInterface) {
        this.callbackInterface = callbackInterface;
    }

    @OverrideprotectedObjectdoInBackground(Object[] objects) {
        dataParsed = "Processed Data";
        returnnull;
    }

    @OverrideprotectedvoidonPreExecute() {
        dataParsed = "";
    }

    @OverrideprotectedvoidonPostExecute(Object o) {
        callbackInterface.onSuccess(dataParsed);
    }
}

While this may seem too much, this is the only way you can communicate between the AsyncTaskClass and the Class which makes use of it. Feel free to ask for any clarifications in case you can't understand. I checked the above answer in my project and it works perfectly.

Post a Comment for "How Can I Fix 'void Android.widget.textview.settext(java.lang.charsequence)' On A Null Object Reference?"