This isn't C++. :) In Java, a variable to an object is a ReferenceType. It's kind of like a pointer in C++. So think of it this way. If you created a pointer variable and passed it around without actually making it POINT to something first, what would happen? You're passing around a NULL pointer. You need to create an object first which you're now doing in onCreate as you've said. Of course you don't need to do this if your code is setup such that it ALWAYS calls SET before GET. It's fine to do a lazy initialization like that I guess.
Chris On Apr 14, 8:57 am, Duskox <[email protected]> wrote: > Thanks that fixed it! > > I created override of OnCreate in my application class and declared a > new ArrayList. The only thing then puzzles me is why I had to do it?? > I thought that it would automatically be filled with the object I > created in my init function that is inside of the initial Activity > class. I was thinking along the lines that this is like a pointer > variable and then I set it with passing a pointer to my object. I > guess it doesn't work that way :D > > Thanks again!! > > On Apr 14, 2:47 pm, Chris <[email protected]> wrote: > > > > > > > > > You never instantiate mGroups in your Application class so unless you > > SET it before GETing it, it's going to be null. > > > Chris > > > On Apr 14, 8:31 am, Duskox <[email protected]> wrote: > > > > Hi all, > > > > so I searched the net and found a nice way to make a global variable > > > by creating a class that extends Application class. I did that. But I > > > am having problems with it. So this is the situation: > > > > public class Group { > > > public String mName; > > > public int mID; > > > public ArrayList<Items> mItems; > > > > } > > > > public class Item { > > > public String mName; > > > > } > > > > public class MyApplicationApp extends Application { > > > private ArrayList<Group> mGroups; > > > > public ArrayList<Group> getGlobalArrayVariable() { > > > return mGroups; > > > } > > > > public synchronized void setGlobalArrayVariable(ArrayList<Group> > > > s) { > > > mGroups = s; > > > } > > > > } > > > > So these are the classes I believe are cruical for this question. Next > > > this is that in my Activity classes I set and get this ArrayList > > > variable. So I set it in my initial Activity class that is called when > > > the application is run. There I fill the array and just to be sure I > > > read it with Log functionality after filling the array in the > > > MyApplicationApp class and everything is ok. > > > > Then I try to read this from another Activity class where I go to > > > after a menu choice. In that activity I successfully read the > > > application, meaning I get a handle to MyApplicationApp object (I > > > guess). This is how I do it: > > > > ... > > > private MyApplicationApp mMyApp; > > > private ArrayList<Group> mGroupsList; > > > > protected void onCreate(Bundle savedInstanceState) { > > > super.onCreate(savedInstanceState); > > > > mMyApp = (MyApplicationApp)this.getApplication(); > > > if (mMyApp == null) Log.i(TAG,"mMyApp == null"); > > > > mGroupsList = > > > (ArrayList<Group>)mMyApp.getGlobalArrayVariable(); > > > if (mGroupsList == null) Log.i(TAG,"1. mGroupsList == > > > null"); > > > > mGroupsList = > > > ((MyApplicationApp)this.getApplication()).getGlobalArrayVariable(); > > > if (mGroupsList == null) Log.i(TAG,"2. mGroupsList == > > > null"); > > > ... > > > > So mMyApp is not null and that is fine I guess, but mGroupsList is > > > null and I presume that is way my app crashes after this last log > > > entry. I guess it crashes because my variable is null?? Anyway even if > > > the reason for crashing is other then this variable being null I am > > > still puzzled why am I not able to get my global variable in this > > > way?? > > > > The reason for two different ways of getting something into > > > mGroupsList variable is because I was trying to see if I am doing it > > > incorrectly but it is the same as far as I know... > > > > Any ideas or pointers what should I do? > > > > Thanks! > > > D. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

