I thought that because i was referencing the activity that it had to be public, but i am still learning so i can try it as private. I just thought that was the way to call on activity funtions, not necessarily making them public, but in that manner nonetheless. interestingly, if i comment out the mSeries.clear() line in the main activity, the service line now moves past and errors out at a DIFFERENT line further down the function, with NPE again. so it seems like it IS in fact calling the function from the service onto the activity.
i've thought about AsyncTask, but didnt think it was the right way to go. currently the graphing works just fine, but as soon as the user leaves focus of the app, i.e. background, obviously all data collection/population stops due to onPause. I wanted to give the user choice of either starting the service to run graph data collection indefinitely, or exit the app shutting down the service to stop data collection. I assumed that AsyncTask would pause just the same if the app was sent to the background in this situation, hence using a service was better. thanks. On Jan 18, 11:12 pm, Jonathan Foley <[email protected]> wrote: > Looking at your code there is something strange that I'm almost > certain is the source of your NPE. > > public RegistersActivity mDisplay = new RegistersActivity(); > > Why are you creating a new activity object? That's not going to be a > reference to the activity object that's visible and hence > any fields that you think are initialized in your activity or either > initialized or null. This is unrelated, but why would you make such a > field public?? Instance fields should almost always be private. > > A service shouldn't access to fields of your activity. The whole point > is for it to be able to do work in the background whether the activity > object is present or not. If you need to do background tasks that are > more intimately connected with the activity use an inner AsyncTask. > > Jonathan > > On Jan 18, 10:44 pm, Roger Podacter <[email protected]> wrote: > > > > > OK so I successfully got my app working with a few real-time graphs. > > I do all of my graph updating in my one activity of my tabbed app. > > However I wanted to now move the updating of my graph data into a > > service such that the data can still be collected and populated while > > the app is in the background, i.e. not in focus. But for the life of > > me I keep getting a null pointer exception error and i can't solve > > it. > > It is difficult to post all of the code in this post, so perhaps i > > can > > link it and just describe what i am attempting. > > > the 2 activities in question are RegistersActivity, which does all > > the > > graph functions, and BatteryCalibratorService, which i want to only > > call on RegistersActivity and update the graph accordingly. > > > with all the code placed in one activity, RegistersActivity, the > > graph > > populates correctly just fine. but as soon as i move my handler and > > runnable to > > my service, and attempt to call on my activity function from the > > service, i get the NPE. specifically the error happens at line 91 > > mDisplay.populateGraph(); of this > > service:http://github.com/R0gerP0dacter/BatteryCalibrator/blob/master/src/net... > > > the error happens simultaneously when i call my populateGraph > > function > > from my service, it errors at line 290 mCurrentSeries.clear(); of the > > activity:http://github.com/R0gerP0dacter/BatteryCalibrator/blob/master/src/net... > > > the errors are linked, i.e. one is caused by the other, but i just > > can't understand why. with all code in one main activity, everything > > works just fine. but as soon as i try to use a service to call on my > > activity function (mDisplay.populateGraph()), i get the null pointer > > exception error. > > > Hope this isn't too much to ask. and yes i am a very novice/ > > beginner/moderate coder, so please forgive if this is a dumb > > question! thanks. > > RP- Hide quoted text - > > - Show quoted text - -- 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

