From the Logcat:
11-26 06:43:40.643: E/AndroidRuntime(1163): FATAL EXCEPTION: AsyncTask #1
...
11-26 06:43:40.643: E/AndroidRuntime(1163): java.lang.RuntimeException: An error occured while executing doInBackground()
...
11-26 06:43:40.643: E/AndroidRuntime(1163): Caused by: java.lang.NullPointerException
...
11-26 06:43:40.643: E/AndroidRuntime(1163): at com.example.mymobiletest.SearchTask.doInBackground(SearchTask.java:134)
Line number 134 is ed = (EditText) mainActivity.findViewById(R.id.mainSearchActivity_editTextSearch);
. Now the execution of this line indicates that mainActivity
(it is an instance of the main activity passed to the constructor of this AsyncTask
) is not null
. So what else could be null
at this line, which is causing the NullPointerException
?
@Override
protected String doInBackground(Void... voidParameters) {
EditText ed=null;
if (mainActivity!=null) {
ed = (EditText) mainActivity.findViewById(R.id.mainSearchActivity_editTextSearch);
} else {
return "mainActivity is the Null culprit.";
}
EDIT:- I do think that since I am not changing the UI in doInBackground()
(but only reading from the UI), so this should not be a problem. But still I tried this in onPreExecute()
since onPreExecute
is executed in the UI thread, but I still get the NPE on the same statement.
@Override
protected void onPreExecute() {
EditText ed=null;
if (mainActivity!=null) {
ed = (EditText) mainActivity.findViewById(R.id.mainSearchActivity_editTextSearch);//******NPE
} else {
Log.i(TAG, "mainActivity is the Null culprit.");
}
searchQuery = ed.getText().toString();
}