views:

122

answers:

2

I seem to be getting a crash after a long period of time on my device:

E/AndroidRuntime( 1115): FATAL EXCEPTION: main
E/AndroidRuntime( 1115): java.lang.RuntimeException: Unable to resume activity {org.stocktwits.activity/org.stocktwits.activity.Main}: java.lang.NullPointerException
E/AndroidRuntime( 1115):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
E/AndroidRuntime( 1115):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
E/AndroidRuntime( 1115):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2059)
E/AndroidRuntime( 1115):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1115):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1115):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 1115):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1115):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 1115):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 1115):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 1115):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1115): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 1115):    at org.stocktwits.activity.Main.getQuotesFromYQL(Main.java:457)
E/AndroidRuntime( 1115):    at org.stocktwits.activity.Main.onStart(Main.java:339)
E/AndroidRuntime( 1115):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
E/AndroidRuntime( 1115):    at android.app.Activity.performStart(Activity.java:3781)
E/AndroidRuntime( 1115):    at android.app.Activity.performRestart(Activity.java:3811)
E/AndroidRuntime( 1115):    at android.app.Activity.performResume(Activity.java:3816)
E/AndroidRuntime( 1115):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
E/AndroidRuntime( 1115):    ... 10 more

onStart() seems to work fine when I exit and resume my app, but fails after some time...

Line 457 in my app is the JSON 'query' object:

/**
     * Performs YQL, parses JSON, and adds Quotes to adapter
     */
    private void getQuotesFromYQL() {
        quotesAdapter.clear();

        System.out.println("YQL QUERY: " + buildQuery());
        JSONObject json = RestClient.connect(buildQuery());
        try {

            JSONObject query = json.getJSONObject("query");
            JSONObject results = query.getJSONObject("results");

            if (query.getString("count").equals("1")) { // YQL JSON doesn't
                // return an array for
                // single quotes
                JSONObject quote = results.getJSONObject("quote");

                Quote myQuote = new Quote();
                myQuote.setName(quote.getString("Name"));
                myQuote.setSymbol(quote.getString("Symbol"));
                myQuote.setLastTradePriceOnly(quote
                        .getString("LastTradePriceOnly"));
                myQuote.setChange(quote.getString("Change"));
                myQuote.setOpen(quote.getString("Open"));
                myQuote.setMarketCapitalization(quote
                        .getString("MarketCapitalization"));
                myQuote.setDaysHigh(quote.getString("DaysHigh"));
                myQuote.setYearHigh(quote.getString("YearHigh"));
                myQuote.setDaysLow(quote.getString("DaysLow"));
                myQuote.setYearLow(quote.getString("YearLow"));
                myQuote.setVolume(quote.getString("Volume"));
                myQuote.setAverageDailyVolume(quote
                        .getString("AverageDailyVolume"));
                myQuote.setPeRatio(quote.getString("PERatio"));
                myQuote.setDividendYield(quote.getString("DividendYield"));

                quotesAdapter.add(myQuote);
            } else {
                JSONArray quotes = results.getJSONArray("quote");
                for (int i = 0; i < quotes.length(); i++) {

                    JSONObject quote = quotes.getJSONObject(i);
                    // .getJSONObject("quote");

                    // Do something with the user
                    Quote myQuote = new Quote();
                    myQuote.setName(quote.getString("Name"));
                    myQuote.setSymbol(quote.getString("Symbol"));
                    myQuote.setLastTradePriceOnly(quote
                            .getString("LastTradePriceOnly"));
                    myQuote.setChange(quote.getString("Change"));
                    myQuote.setOpen(quote.getString("Open"));
                    myQuote.setMarketCapitalization(quote
                            .getString("MarketCapitalization"));
                    myQuote.setDaysHigh(quote.getString("DaysHigh"));
                    myQuote.setYearHigh(quote.getString("YearHigh"));
                    myQuote.setDaysLow(quote.getString("DaysLow"));
                    myQuote.setYearLow(quote.getString("YearLow"));
                    myQuote.setVolume(quote.getString("Volume"));
                    myQuote.setAverageDailyVolume(quote
                            .getString("AverageDailyVolume"));
                    myQuote.setPeRatio(quote.getString("PERatio"));
                    myQuote.setDividendYield(quote.getString("DividendYield"));

                    quotesAdapter.add(myQuote);
                }
            }

            serializeQuotes();

        } catch (JSONException e) {
            System.out.println(e);
        }
    }
A: 

The error speaks by itself: NullPointerException. Just try to see why are you getting a null value there, and try to catch it also...

 catch (JSONException e) {
            System.out.println(e);
 }
 catch (NullPointerException n) {
            // do something useful here
 }
Cristian
I can prevent the crash by catching the nullpointerexception, but from the answer above, it looks like the OS is killing my process and skipping onCreate(). How can I handle this properly?
Sheehan Alam
It doesn't skip onCreate() if it kills the process. It *has* to do onCreate() to display the current activity after that, because it will need to create the activity. It just may create activities in a different order than originally, because they are only created as they are shown. You just need to deal with this. (And to the original comment -- just catching NullPointerException is at best a band-aid if not outright wrong; the code just needs to deal correctly with things so this doesn't happen.)
hackbod
is it hacky to check for null in onStart() or onResume() and re-initialize there?
Sheehan Alam
Not at all... in fact that's what you have to do :D
Cristian
+1  A: 

I experienced similar problems earlier. It is not that Android kills some variable, but because after some time Android kills the process, when you start the program again, the program is started not on the first activity, but the last activity you were on. Therefore you may get some variables that you usually have them initialized on the first activity not initialized.

yuku
how did you resolve this?
Sheehan Alam
Initialize the variables on the onCreate of each activity, not only the main activity.
yuku