0

Im trying to create a list here, but the issue is that after i fill the array daysList with information, everything is lost after i exit onResponse.

If i set the textview inside the onResponse method it works, but it does not if i do it outside (like in this case), i get null pointer exception.

I understand that i have to define the array as static to keep the data, but it's not working either! How do i fix this?

public class MainFragment extends Fragment {

private View view;
private RequestQueue requestQueue;
private WeatherAdapter weatherAdapter;
public static WeatherForecast weatherForecast;
public static WeatherForecast.List[] daysList;

private ListView listView;
private TextView textView;

String URL = "requestURL";

public void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); }

public View onCreateView ( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    view = inflater.inflate(R.layout.main_fragment,null);

    textView = (TextView) view.findViewById(R.id.textview);

    requestQueue = Volley.newRequestQueue(getActivity());
    JsonObjectRequest jsonWeatherRequest = new JsonObjectRequest(
            URL,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    weatherForecast = parseWeatherToJson(response.toString());
                    daysList = weatherForecast.getList();
                    //textView.setText("Humidity is: " + daysList[0].getHumidity());  <--- If i set it here it works!
                    weatherAdapter.notifyDataSetChanged();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.v(TAG, "On Error!");
                }
            }
    );

    requestQueue.add(jsonWeatherRequest);

    textView.setText("Humidity is: " + daysList[0].getHumidity()); // <---- This throws nullpointer.
}
Emiliano Rodriguez
  • 454
  • 2
  • 6
  • 19
  • Where is `weatherAdapter` set? – Stephen C Jun 05 '16 at 00:17
  • weatherAdapter is a custom ArrayAdapter that i made to display this array, but i cant get the information outside the onResponse method. I deleted the adapter lines of code, notifyDataSetChanged should also be withdrawn. – Emiliano Rodriguez Jun 05 '16 at 00:22

3 Answers3

1

If i set the textview inside the onResponse method it works, but it does not if i do it outside

Net request is an asynchronous operation, so you have to write all the logic in the methods of callback object. That's why you have data inside the callback object, but you haven't them ouside.

  • Yes i added the adapter logic inside of the onResponse method and its working, but i dont think thats the standar way of doing this. It would be good to be able to use the array outside that method. Thanks ! – Emiliano Rodriguez Jun 05 '16 at 00:47
  • Using net response data right after request is scynchronous. If you do that in application or activity, you'll got NetworkOnMainThread exception. Standard way (like, for example, in JS) is to work in callback object. – science.saf Jun 05 '16 at 01:08
  • Oh i see! I think i know how to fix this. I will update tomorrow with a new solution! – Emiliano Rodriguez Jun 05 '16 at 01:22
1

jsonWeatherRequest is a async request which hits the server and gets the response in onResponse() callback. When you are setting the TextView with the list value outside onResponse(), at that moment your request is not completed and thus your list is null.

So if you want to access the list, then you have to do that in onResponse() callback.

Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
  • I just set the adapter inside the inResponse method and the list displays correctly. But no one does it that way on the thousand examples i read through. Is there a way to use the array information outside the onResponse method? Thanks! – Emiliano Rodriguez Jun 05 '16 at 00:45
  • I don't know which examples you are taking about. It may be possible those examples have different requirements.If you want to use the data outside the onResponse, you need to implement a different listener which you'll will invoke in onResponse with the data. – Shadab Ansari Jun 05 '16 at 03:17
0

Is there a way to use the array information outside the onResponse method?

Yea. Sure there is.

But you need to wait for the response to complete first!!

That is what is missing from your code.

This Q&A explains how to wait for a Volley request to complete:

But BEWARE that if you wait in the wrong place you are liable to lock up your app's user interface.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216