lopoly
lopoly

Reputation: 23

Passing data from onPostExecute() to adapter

Can't pass data from onPostExecute() to adapter for my AutoComleteTextView. Logcat shows me:

An exception occurred during performFiltering()! java.lang.NullPointerException: collection == null.

public class UzActivity extends Activity {

private static final String DEBUG_TAG = "HttpExample";
List<String> responseList;

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

    final String url = "http://booking.uz.gov.ua/purchase/station/%D0%9A%D0%B8%D0%B5/";
    new FetchStationTask().execute(url);





   AutoCompleteTextView textView = (AutoCompleteTextView)
            findViewById(R.id.autoCompleteTextView1);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, responseList);

    textView.setAdapter(adapter);


}

private class FetchStationTask extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... urls) {
        try {
            return new UzFetcher().getUrlString(urls[0]);
        } catch (IOException e) {
            return "Unable to retrieve web page. URL may be invalid.";
        }
    }

    @Override
    protected void onPostExecute(String result){

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            StationResponse st = objectMapper.readValue(result, StationResponse.class);
            responseList = new ArrayList<>();
            for (int i = 0; i<st.mStations.size(); i++){

                    responseList.add(st.mStations.get(i).getTitle());

            }


            Log.i(DEBUG_TAG, responseList.get(0));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i(DEBUG_TAG, result);

    }
}

Upvotes: 0

Views: 319

Answers (1)

Yuva Raj
Yuva Raj

Reputation: 3881

java.lang.NullPointerException: collection == null.

ArrayList should be initialized first.

Just add,

responseList = new ArrayList<String>();

after setContentView();

Upvotes: 2

Related Questions