Question

I'm parsing a JSON response from a weather feed and the data I receive includes a url to an icon that corresponds with the conditions, then I display that icon in an ImageView. I was able to make this work in a sandbox app before moving the package of code into my project.

In the main project I use the latest versions of ActionBarSherlock and SlidingMenu for navigation, and both apps make use of Android-Async-Http-Client but now it has a fatal NullPointerException when trying to display the icon from that url in the ImageView. I don't think this is caused by any of the libraries.

I've seen that sliding menus mostly consist of some type of ListView handled with an Adapter, which I'm doing in a portion of my sliding menu. But the only clue I have is that it crashes because now it's an off-screen UI element that I'm trying to work with. Does it matter that I have something in my sliding menu that isn't handled by an Adapter? I can't imagine much else is different but maybe you guys some clue of what I'm missing. Hopefully it isn't something basic that I'm just forgetting... or maybe it is :)

Alright, thanks for any ideas.

wireframe

public class MainActivity extends SlidingActivity implements
        ActionBar.OnNavigationListener {
    final String TAG = "MainActivity";
    private TextView mSelected;
    private String[] mLocations;
    ImageLoader imageLoader;
    ImageView weatherIcon;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageLoader = new ImageLoader(this);

        wireViews();

        getWeatherFeed();

        Context context = getSupportActionBar().getThemedContext();
        ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(
                context, R.array.activities, R.layout.sherlock_spinner_item);
        list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        getSupportActionBar().setListNavigationCallbacks(list, this);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        // set the Behind View
        setBehindContentView(R.layout.menu_frame);

        // SlidingMenu
        SlidingMenu sm = getSlidingMenu();
        sm.setShadowWidthRes(R.dimen.shadow_width);
        sm.setShadowDrawable(R.drawable.shadow);
        sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        sm.setFadeDegree(0.35f);
        sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setSlidingActionBarEnabled(false);

        ListView menuList = (ListView) findViewById(R.id.menuList);
        SlidingMenuItem[] data = new SlidingMenuItem[] {
                new SlidingMenuItem("Item1 Label", "Item1 Sub-label",getResources().getDrawable(R.drawable.item1)),
                new SlidingMenuItem("Item2 Label", "Item2 Sub-Label",getResources().getDrawable(R.drawable.item2)) };

        MenuAdapter adapter = new MenuAdapter(this, R.layout.sliding_menu_item,
                data);
        menuList.setAdapter(adapter);
    }

    private void updateWeatherUi(String degreeText, String descriptionText,
            String iconUrl) {
        Log.i(TAG, "Updating UI elements...");

        if (imageLoader != null) {
            Log.i(TAG, "IMAGE_LOADER NOT NULL!!!");
        }
        imageLoader.DisplayImage(iconUrl, weatherIcon);   // FIXME: FATAL CRASH HERE !!!
    }

    /** Build a Weather object from the JSON response.
     * @throws JSONException */
    private void buildWeatherObject(String rawResponse) throws JSONException {
        Log.i(TAG, "Building Weather ojbect...");

        JSONObject baseObject = new JSONObject(rawResponse);
        JSONObject data = new JSONObject(baseObject.getString(Key.DATA));

        JSONArray conditionArray = new JSONArray(
                data.getString(Key.CURRENT_CONDITION));

        for (int i = 0; i < conditionArray.length(); i++) {
            JSONObject conditionElement = new JSONObject(
                    conditionArray.getString(i));
            weather = new Weather();
            weather.tempMaxF = conditionElement.getString(Key.TEMP_F);

            JSONArray weatherDescriptionArray = new JSONArray(
                    conditionElement.getString(Key.W_WEATHER_DESC));
            for (int j = 0; j < weatherDescriptionArray.length(); j++) {
                JSONObject weatherDescriptionElement = new JSONObject(
                        weatherDescriptionArray.getString(j));
                weather.weatherDesc = weatherDescriptionElement
                        .getString(Key.VALUE);
            }

            JSONArray weatherIconArray = new JSONArray(
                    conditionElement.getString(Key.WEATHER_ICON_URL));
            for (int j = 0; j < weatherIconArray.length(); j++) {
                JSONObject weatherIconElement = new JSONObject(
                        weatherIconArray.getString(j));
                weather.weatherIconUrl = weatherIconElement
                        .getString(Key.VALUE);
                Log.i(TAG, weather.weatherIconUrl);
            }
            conditionElement = null;

            updateWeatherUi(weather.tempMaxF, weather.weatherDesc,
                    weather.weatherIconUrl);
        }
    }

    /** Asynchronously request and receive weather feed data. */
    private void getWeatherFeed() {
        Log.i(TAG, "Getting asynchronous JSON feed...");

        AsyncHttpClient client = new AsyncHttpClient();
        client.get(WEATHER_FEED_URL, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) {
                try {
                    buildWeatherObject(response);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Throwable arg0, String arg1) {
                super.onFailure(arg0, arg1);
                showToast("Sorry, the weather forecast wasn't available just then.");
            }
        });
    }

    private void wireViews() {
        Log.i(TAG, "Wiring UI elements...");
        mSelected = (TextView) findViewById(R.id.text);
        mLocations = getResources().getStringArray(R.array.activities);
        weatherIcon = (ImageView) findViewById(R.id.weatherIconView);
    }

12-21 01:01:11.130: I/MainActivity(28502): Building Weather ojbect...
12-21 01:01:11.170: I/MainActivity(28502): http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png
12-21 01:01:11.170: I/MainActivity(28502): Updating UI elements...
12-21 01:01:11.170: I/MainActivity(28502): IMAGE_LOADER NOT NULL!!!
12-21 01:01:11.170: D/AndroidRuntime(28502): Shutting down VM
12-21 01:01:11.170: W/dalvikvm(28502): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
12-21 01:01:11.180: E/AndroidRuntime(28502): FATAL EXCEPTION: main
12-21 01:01:11.180: E/AndroidRuntime(28502): java.lang.NullPointerException
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.eric.app.networkxml.ImageLoader.DisplayImage(ImageLoader.java:42)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.eric.app.MainActivity.updateWeatherUi(MainActivity.java:129)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.eric.app.MainActivity.buildWeatherObject(MainActivity.java:172)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.eric.app.MainActivity.access$0(MainActivity.java:137)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.eric.app.MainActivity$1.onSuccess(MainActivity.java:195)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.loopj.android.http.AsyncHttpResponseHandler.handleSuccessMessage(AsyncHttpResponseHandler.java:160)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:173)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.loopj.android.http.AsyncHttpResponseHandler$1.handleMessage(AsyncHttpResponseHandler.java:85)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at android.os.Looper.loop(Looper.java:150)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at android.app.ActivityThread.main(ActivityThread.java:4263)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at java.lang.reflect.Method.invokeNative(Native Method)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at java.lang.reflect.Method.invoke(Method.java:507)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at dalvik.system.NativeStart.main(Native Method)

Yikes! I tried to post a "snippet" but maybe I should've posted the complete file...

Was it helpful?

Solution

Putting the updateWeatherUi() logic at inside the end of buildWeatherObject() fixed this for me.

The problem is that I'm still not sure what was occurring here previously. The Logs were showing the proper data right up to the point of imageLoader.DisplayImage(String, ImageView) in my original code but I kept crashing there with NullPointerException.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top