Reputation: 51
Ok so I know I should use interfaces to retrieve information from AsyncTask because they run in separate threads but I have been trying in vain to get this right. Can someone guide me to what is wrong with my code?
First I thought I was getting null pointer exception at callback.onJsonReady(movie)
because I was still trying to reach movie that was not processed despite my attempt. So I tried sleeping it for 5000ms before it retrieves but I still get a null pointer exception. please help me.
If you need to look at more of my code, please tell me also.
Below is the method where I set up my interface
protected void onPostExecute(String s) {
super.onPostExecute(s);
processData(getmData());
}
private void processData (String mData){
try {
final String MOVIE_TITLE = "Title";
JSONObject jsonObject = new JSONObject(mData);
Log.v(LOG_TAG, mData);
String title = jsonObject.getString(MOVIE_TITLE);
Movie movie = new Movie(title);
callback.onJsonReady(movie);
Log.v(LOG_TAG, "Title of the movie is " + movie.getTitle());
}catch (JSONException e){
Log.e(LOG_TAG, "Error retrieving JsonData");
e.printStackTrace();
}
}
}
Below is my class where I call the interface
public class ResultsPage extends AppCompatActivity implements ParseJsonData.ParseJsonCallback{
private final String LOG_TAG = getClass().getSimpleName();
private TextView title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results_page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTextViews();
}
private void setTextViews () {
Bundle bundle = getIntent().getExtras();
String movieTitle = bundle.getString("title");
Log.v(LOG_TAG, "title recieved is : " + movieTitle);
ParseJsonData parseJsonData = new ParseJsonData(movieTitle, this);
parseJsonData.execute();
}
@Override
public void onJsonReady(Movie movie) {
title.setText(movie.getTitle());
}
}
My logcat is
03-14 18:03:02.931 30827-30827/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jc.tagyourmovie, PID: 30827
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.jc.tagyourmovie.ResultsPage.onJsonReady(ResultsPage.java:44)
at com.jc.tagyourmovie.ParseJsonData$ParseJsonDataBackground.processData(ParseJsonData.java:83)
at com.jc.tagyourmovie.ParseJsonData$ParseJsonDataBackground.onPostExecute(ParseJsonData.java:69)
at com.jc.tagyourmovie.ParseJsonData$ParseJsonDataBackground.onPostExecute(ParseJsonData.java:53)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access$500(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Upvotes: 0
Views: 72
Reputation: 27549
Error is in title.setText
line. Logcat says so >
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
Seems you haven't initialize your textview
named title
, initialize it before using it.
Your code declared a textview
named title
private TextView title;
you have to initialize this textview in onCreate() like below
title = (TextView)findViewById(R.id.TEXTVIEWID);
Upvotes: 1
Reputation: 7479
private TextView title;
You need to give a value to title, something like this:
title = (TextView) R.findViewById(R.id.textviewid);
And after that you can call setText()
on it.
Replace textviewid with your textview's id in xml file.
Upvotes: 0
Reputation: 48288
Your Textview title is not initialized... that is the reason
you need to do something like:
title = (TextView)findViewById(R.id.<your_tv_id>);
Upvotes: 2
Reputation: 5619
You need to move code that changes UI to the onPostExecute part of the AsyncTask.
callback.onJsonReady(movie);
This part can be in the doInBackground, but use a field of the AsynchTask rather than a local var so you can access it in the onPostExecute:
Movie movie = new Movie(title);
Upvotes: 0