Search code examples
javaandroidandroid-fragmentsandroid-asynctaskreturn

Problem with returning object form a class extending AsyncTask


I'm having a problem with getter function returning object from a class HttpRequestCurrentWeather extending AsyncTask. In this class I receive data from API, and want to set some TextViews in CurrentWeatherFragment depending on what I received. The problem is I can't return the whole object of DataModel to the Fragment and set the components there nor set TextViews is onPostExecute() method, because I'm getting a NullPointerException ( java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference). What should I do to set those TextViews?

public class CurrentWeatherFragment extends Fragment {
    DataModel dataModel = new DataModel();

    TextView tv_city;
    TextView tv_temperature;
    TextView tv_pressure;

    public void setComponents() {
        this.tv_city = getView().findViewById(R.id.tv_current_city_name);
        tv_city.setText(dataModel.getName());

        this.tv_temperature = getView().findViewById(R.id.tv_current_temperature);
        tv_temperature.setText(dataModel.getTemp());

        this.tv_pressure = getView().findViewById(R.id.tv_current_pressure);
        tv_pressure.setText(dataModel.getPressure());
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View RootView = inflater.inflate(R.layout.fragment_current_weather_layout, container, false);


        HttpRequestCurrentWeather httpRequestCurrentWeather1 = new HttpRequestCurrentWeather();
        httpRequestCurrentWeather1.execute("", "", "");

        // here I receive null object, but why?
        dataModel = httpRequestCurrentWeather1.getDataModel();

        // set the texts in components:
        setComponents();

        return RootView;
    }
}
public class HttpRequestCurrentWeather extends AsyncTask<String, Void, DataModel> {
        DataModel dataModel;

        public HttpRequestCurrentWeather() {
            this.dataModel = null;
        }

        @Override
        protected DataModel doInBackground(String... params) {
            ApiCurrentWeather apiCurrentWeather1 = new ApiCurrentWeather("London", "uk");

            try {
                // catch the model with recived data from API:
                dataModel = apiCurrentWeather1.getWeather();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return dataModel;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(DataModel dataModel) {
        super.onPostExecute(dataModel);

        CurrentWeatherFragment fragment = new CurrentWeatherFragment();

        TextView tv_temperature = (TextView)fragment.getView().findViewById(R.id.tv_current_temperature);
        tv_temperature.setText(dataModel.getTemp());

    }

    // this function returns NULL object ????
    public DataModel getDataModel(){
        return this.dataModel;
    }
}

Solution

  • To be honest, this is not how the appropriate answer should look like, but what I was trying to achieve using AsyncTask seemed far more easier using Retrofit, so I finally gave up this idea and decided to use Retrofit instead. If you have similar problem, forget about AsyncTask - Retrofit is more suitable for this!