0
When I try to use the code below, (what alias I managed to get with help from friends of the OS) an error appears on the line:
spinner.setOnItemSelectedListener(this);
With the following console code:
java.lang.Runtimeexception:
Unable to start Activity Componentinfo{ jp.co.e_grid.rakuseki/jp.co.e_grid.rakuseki.Postconfirmationactivity
}:
java.lang.Nullpointerexception:
Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null Object Reference
Here is my code:
public class JSONOAsyncTask extends AsyncTask<String, Void, Boolean> implements AdapterView.OnItemSelectedListener{
private Spinner spinner;
private ArrayAdapter<String> dataAdapter;
@Override
protected void onPreExecute() {
super.onPreExecute();
Spinner spinner = (Spinner) findViewById( R.id.memo_confirmation_spinner );
spinner.setOnItemSelectedListener(this);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
Log.e("****** MESSAGE ******", " Json Object = " + JSONParser.getJSONFromUrl( URL ).get("ReportDetailTextList"));
} catch (JSONException e) {
e.printStackTrace();
}
// TEST: Temporaly list
List<String> categories = new ArrayList<String>();
categories.add("aaa");
categories.add("bbb");
categories.add("ccc");
categories.add("ddd");
categories.add("eee");
// Adapter Creation
// ArrayAdapter adapter = ArrayAdapter.createFromResource(getBaseContext(), R.array.confirmation_memo, android.R.layout.simple_spinner_item);
// adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
return false;
}
protected void onPostExecute(Boolean result) {
// putting adapter in to data
spinner.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Important detail:
When I call the same "spinner" from within the Oncreate() with that same ID, everything works fine, see code snippet:
Spinner spinner = (Spinner) findViewById(R.id.memo_confirmation_spinner);
spinner.setOnItemSelectedListener(this);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.confirmation_memo, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
I have read other posts about "null Object Reference" in the SO but it seems to me that there are many variations of the same error, so here is my question: What this error message refers to and, how to resolve it?
Look, some object you’re calling isn’t being initialized, now just figure it out. The correct would be you "Debug" until you find the object that fires the exception.
– Wilson Santos
when you speak thresh, would put "stop in code" points and I would follow step-by-step?
– Camila Yamamoto
Um... I don’t know much about Java, but when you call the snippet "spinner.setOnItemSelectedListener(this)" maybe the spinner object is null, hence it implies that the above call "(Spinner spinner = (Spinner) findViewById( R.id.memo_confirmation_spinner );" did not find the object by Id (if that’s what the function does)
– Wilson Santos
Yes, Camila. It’s a good start, at least you can see the state of your object and when it will trigger the error.
– Wilson Santos
yes!! debugged! (thanks for the tip! ) D The spinner really comes "null", but the same code, inside the "onCreate" works smoothly! what can it be? about it not being able to "open" the spinner? be inside a Asynctask?? (I’ve even corrected the question!)
– Camila Yamamoto
I think it’s unlikely because it’s an asynchronous call. You have to see first if the Id really is the same, it may be going as 0 or null within the preOnExecute, so it does not find an object.
– Wilson Santos
got @Wilsonsantos, but it turns out that in Androidstudio, it is not possible to put a wrong id, it already accuses the error at the time you type, and I also confirmed calmly if it is written right, not to mention that I re-wrote the code inside onCreate() to test, and then it works! with the same id , inclusive...
– Camila Yamamoto