android


Android: member variable becoming null


I have an activity - MainActivity, where I initialize member variable in onCreate(...) method. This variable is then used in onStart() and onStop() methods.
Pretty simple, huh? Now the issue is, that from my users, I started to get NullPointerException crash reports, which happens in onStop() method.
I've been extensively searching for the cause, and I understand that static variables may become null when android decides to free up memory, however I couldn't find a case with member variable, which is initialized in onCreate().
The code is following:
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener, SharedPreferences.OnSharedPreferenceChangeListener {
private MySvc mySvc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mySvc = DI.i().getMySvc();
...
}
#Override
protected void onStart() {
super.onStart();
mySvc.start();
}
...
#Override
protected void onStop() {
super.onStop();
//here I get NPE
mySvc.stop();
}
}
}
To make things even more mysterious, I started to get this crash reports only from certain versions of app, however the old versions have same behavior in onStart() and onStop().
I will be grateful for any hint.
Edit:
DI.i().getMySvc(); never returns null, its done according to singleton pattern.
MySvc is a POJO, which is instantiated in DI class
I need to appologize, I've found out that the issue is caused by our code. From certain version we added internet connection check to onCreate() method like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isInternetConnectionPresent()) {
return;
}
mySvc = DI.i().getMySvc();
...
}
So if users were offline, they got NPE, since variables were never initialized, however onStop() was called.
I think the root cause is wrong approach to error handling. What I would like to achieve is to be able to throw exceptions from activity, and have generic handlers, which would be able to handle those exceptions.
So in this case I would throw NoInternetConnectionException and a handler would display a meaningful dialog to the user.
Also I wonder if other lifecycle methods are called when an exception is thrown.
Depending on what MySvc is, android VM may choose to GC it as it needs. The best approach with android is to first check for null, instantiate it again if needed, and then do what you want. Works for me all the time and no performance issues.

Related Links

Can I save the photo to RAM in Android without sd card
Wrong viewport height
Android - How to calculate tilt angles from accelerometer data?
Header image can't fit correctly (DrawerLayout)
How to use microsoft's thrifty over http?
TLS in Smack when connecting to XMPP server
For Audiorecord, what is relationship between record time with record length(in bytes)?
Could not find class 'android.os.PersistableBundle', referenced from method com.sample.loadermanegersample.MainActivity.access$super
Trying to set alpha with data binding from another view's visibility
Leak : Timer and TextWatcher
Null Pointer Exception widget button
Android unable to apply proguard into ndk based project
How to save only the first fragment
WebView can't handler url which contains chinese?
Should we pass a copy of the items to RecyclerView, so that when we filter, we don't remove from the actual list?
Check if the app was launched by a web browser

Categories

HOME
android
algorithm
notepad++
httparty
ecmascript-6
nunit
solaris
owl-carousel
vscode-extensions
cq5
maxscript
x264
android-json
pkcs#11
qpython
google-chrome-app
speech-synthesis
azure-iot-hub
alpacajs
package.json
vuejs
swagger-2.0
ksh
gulp-watch
monit
google-cloud-print
jasmine-node
errbot
openmdao
browser-sync
dumpbin
batching
fiware-cygnus
searchkit
cd
xdocreport
health-monitoring
cin
google-data-api
tasm
attributeerror
naming
android-checkbox
importerror
wc
stylecop
strftime
phpdbg
vaadin-elements
arq
video-conferencing
monkey
django-1.10
shinobi
visual-studio-2008-sp1
ziparchive
microstation
xcode-server
myspace
appscale
php-socket
cda
beanstalk
blitline
mobiledoc-kit
phpgrid
rasterize
custom-errors
java-money
lazybones
ess
sapscript
viola-jones
baasbox
avahi
with-statement
angular-carousel
android-framework
ora-06550
cnf
dolby-audio-api
cardreader
cascadingdropdown
sqlclr
intersect
redis-py
jquery-blockui
koken
google-maps-mobile
agility.js
multiplatform
object-database
ajax-polling
lobo-cobra
dsoframer
pbcopy
appliance

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App