-1

I have been following the course on udacity and have nearly finished lesson 2. The program should be updating the list view with the now parsed data on the JSON. Except when the following code is added the program crashes:

@Override
    protected void onPostExecute(String[] result) {
        if (result != null) {
            forecastAdapter.clear();
            for (String dayForecastStr : result) {
                forecastAdapter.add(dayForecastStr);
            }
        }
    }

If that code is not there the program runs as usual and logs the results of the json parse. Others have suggested to

"Make sure you don't redefine the mForecastAdapter in "onCreateView" and only have : mForecastAdapter = new ArrayAdapter"

I'm unsure about how to go ahead of this. I already have mForecastAdapter = new ArrayAdapter<string> at the head of my class. And the rest of the code on the oncreateview is just loading the array into the listview.

The error log I currently have is:

11-05 19:29:00.049 20310-20553/com.example.jeremy.sunshine V/FetchWeatherTask﹕ Forecast entry: Wed, Nov 5 - Clear - 7/7 11-05 19:29:00.049 20310-20553/com.example.jeremy.sunshine V/FetchWeatherTask﹕ Forecast entry: Thu, Nov 6 - Rain - 12/9 11-05 19:29:00.049 20310-20553/com.example.jeremy.sunshine V/FetchWeatherTask﹕ Forecast entry: Fri, Nov 7 - Rain - 12/10 11-05 19:29:00.049 20310-20553/com.example.jeremy.sunshine V/FetchWeatherTask﹕ Forecast entry: Sat, Nov 8 - Rain - 12/11 11-05 19:29:00.049 20310-20553/com.example.jeremy.sunshine V/FetchWeatherTask﹕ Forecast entry: Sun, Nov 9 - Rain - 15/11 11-05 19:29:00.049 20310-20553/com.example.jeremy.sunshine V/FetchWeatherTask﹕ Forecast entry: Mon, Nov 10 - Clear - 15/12 11-05 19:29:00.049 20310-20553/com.example.jeremy.sunshine V/FetchWeatherTask﹕ Forecast entry: Tue, Nov 11 - Clear - 15/11 11-05 19:29:00.049 20310-20310/com.example.jeremy.sunshine D/AndroidRuntime﹕ Shutting down VM 11-05 19:29:00.049 20310-20310/com.example.jeremy.sunshine W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415c5db8) 11-05 19:29:00.049 20310-20310/com.example.jeremy.sunshine E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.jeremy.sunshine, PID: 20310 java.lang.NullPointerException at com.example.jeremy.sunshine.ForecastFragment$FetchWeatherTask.onPostExecute(ForecastFragment.java:305) at com.example.jeremy.sunshine.ForecastFragment$FetchWeatherTask.onPostExecute(ForecastFragment.java:108) at android.os.AsyncTask.finish(AsyncTask.java:632) at android.os.AsyncTask.access$600(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5146) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) at dalvik.system.NativeStart.main(Native Method)

My whole class can be seen here:

As this is my first android project I'm baffled to why this won't work. I presume its something I must be overlooking.http://pastebin.com/x3rcqbdS

slava
  • 1,901
  • 6
  • 28
  • 32
Jeremy
  • 447
  • 1
  • 4
  • 12

3 Answers3

3

When you initialize your forecastAdapter in onCreateView with the following code:

ArrayAdapter<String> forecastAdapter =
            new ArrayAdapter<String>(

You are creating a new instance of forecastAdapter that is only within the scope of onCreateView. Then when you go to use your forecastAdapter in onPostExecute the forecastAdapter that is within the scope of onPostExecute is null because it was never initialized, the other instance was though (as Elliott Frisch mentioned, who just deleted his answer).

You should be able to just delete the ArrayAdapter<String> from your onCreateView adapter and it will work.

TronicZomB
  • 8,667
  • 7
  • 35
  • 50
  • 1
    You were right that part did have to go but I also needed to change the array into a public instead of private! Thanks a lot – Jeremy Nov 05 '14 at 19:59
0

I was having the same problem. The solution is that you must not be adding list to the mForecastAdapter because according to the old code we add String[] rather than the weekForecast list. So make sure that this line is proper:

mForecastAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, R.id.list_item_forecast_textview,weekForecast);
slava
  • 1,901
  • 6
  • 28
  • 32
Rohan Chauhan
  • 65
  • 1
  • 9
0

I had had this problem before. The array adapter is not null as I can access its count, but I can't clear it or add elements to it. This is because the way I have initialised my array adapter in onCreateView() method. I passed it the last argument as "Arrays.asList(my_array_of_strings)"

String[] data = {
            "Mon 6/23 - Sunny - 31/17",
            "Tue 6/24 - Foggy - 21/8",
            "Wed 6/25 - Cloudy - 22/17",
            "Thurs 6/26 - Rainy - 18/11",
            "Fri 6/27 - Foggy - 21/10",
            "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
            "Sun 6/29 - Sunny - 20/7"
    };

arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast,
            R.id.list_item_forecast_textview, Arrays.asList(data));

while the last argument should be "new ArrayList(Arrays.asList(my_array_of_strings))"

String[] data = {
            "Mon 6/23 - Sunny - 31/17",
            "Tue 6/24 - Foggy - 21/8",
            "Wed 6/25 - Cloudy - 22/17",
            "Thurs 6/26 - Rainy - 18/11",
            "Fri 6/27 - Foggy - 21/10",
            "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
            "Sun 6/29 - Sunny - 20/7"
    };
    List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));

    arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast,
            R.id.list_item_forecast_textview, weekForecast);

this is because the difference between Arrays.asList(array) and new ArrayList(Arrays.asList(array)) as stated here .

Community
  • 1
  • 1