Saturday, October 8, 2011

[android-developers] Re: unreasonable NullPointerException in onDestroy()

My bad, I just realised I do call finish in onCreate and return before
the variable is initialised. Kostya explained most of it. Thanks.

On Oct 8, 6:30 pm, Kostya Vasilyev <kmans...@gmail.com> wrote:
> Calling finish() from onCreate still results in onDestroy getting called.
>
> So if your onCreate calls finish() and returns -- before you initialize
> the list -- perhaps as part of displaying the license agreement, or
> something else along those lines -- then in onDestroy the list will
> still be null. Kaboom!
>
> So, +1 to Studio's advice of an if(!null) check. Or else create the list
> as a member initializer:
>
> class MyActivity {
>      private List<String> list = new ArrayList<String>();
>      ....
>
> }
>
> -- Kostya
>
> 08.10.2011 21:12, Studio LFP ?????:
>
>
>
>
>
>
>
>
>
> > Without code samples, I don't think anyone is going to be able to help
> > you find your issue. I use this all the time and the only time I have
> > seen it not work was when I made a mistake somewhere else in the code.
>
> > I do usually do a null check on things in the onDestory before I use
> > them because my code can get more complicated as it goes. I end up
> > changing variables along the way forgetting I'm cleaning them up at
> > the end.
>
> > If you do find a serious bug like this, you'd definitely want to go
> > through the steps to reproduce it and post a bug reports in the
> > Android Issue Tracker if someone else hasn't found it already.
>
> >http://code.google.com/p/android/issues/list
>
> > Steven
> > Studio LFP
> >http://www.studio-lfp.com
>
> > On Saturday, October 8, 2011 11:53:05 AM UTC-5, DraganA wrote:
>
> >     That's the problem, the list is created in onCreate() method and is
> >     not reassigned or set to null anywhere. I forgot to say, the null
> >     pointer happens in onDestroy() method in list.clear() line. From that
> >     I can only deduce that onDestroy() is called before onCreate()
>
> >     On Oct 8, 5:42 pm, Studio LFP <studi...@gmail.com> wrote:
> >     > Based on that, you shouldn't get a crash, but obviously that's
> >     not the full
> >     > code you are using.
>
> >     > Are you changing that variable at any other point in the code?
>
> >     > Steven
> >     > Studio LFPhttp://www.studio-lfp.com<http://www.studio-lfp.com>
>
> >     > On Saturday, October 8, 2011 3:34:15 AM UTC-5, DraganA wrote:
>
> >     > > I'm puzzled as to why I'm getting Null pointer exception in the
> >     > > construct below, knowing the Activity life cycle, it shouldn't
> >     happen.
> >     > > Could the clue be in TabActivity??
>
> >     > > public class Main extends TabActivity {
>
> >     > >     private List<String> list;
>
> >     > >     public void onCreate(Bundle savedInstanceState) {
> >     > >         list = new ArrayList<String>();
> >     > >     }
>
> >     > >     protected void onDestroy() {
> >     > >         list.clear();
> >     > >     }
> >     > > }
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to android-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscribe@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
> --
> Kostya Vasilyev

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment