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

Replace the /system/lib/libstagefright.so in Android and can't boot
Cannot resolve local Android application crash
Android: Set minimum height of a view programmatically
Application semantics for downloading an Android APK expansion file
Android - Set gravity of elements in XML
How to make speech recognizer rwork on whole screen
Draw rubber band line with an ontouchevent
set file permission to android
unprefixed attribute is giving error in style.xml
how to set array in listview android [closed]
how to start android phone with custom launcher
Android ImageAdapter setImageResource
Google Drive client/server app
Android + Google API v2 + app not launched
Android task manager or system dialog
SimpleCursorAdapter error [closed]

Categories

HOME
design-patterns
fuelux
gdb
codenvy
telegram
angular2-material
jmx
apache-spark-mllib
google-apps
3d-reconstruction
jetty
gluon-mobile
extjs4.2
owin
octave
android-securityexception
frequency-distribution
symbol
breadth-first-search
ksh
imagemap
r-mice
mongoid6
direct3d11
onload
pic32
syntax-highlighting
jquery-multiselect
gpio
bulletphysics
licensing
mat
browser-sync
livelink
jmockit
twos-complement
latitude-longitude
xcode7.3
apns-php
dcmtk
congestion-control
photos
memory-mapping
textsum
reportservice2010
datamaps
bluez
posixct
java-6
git-flow
flyout
latin
spring-cloud-feign
mta
i386
synonym
cfchart
aurora
exim4
vhosts
dynamics-nav-2016
cgi-bin
sony-lifelog-api
mod-proxy
uncompress
dronekit-android
rdflib
android-broadcast
adehabitathr
reflector
matrix-inverse
ios8-extension
extjs2
getopt
xmltodict
high-resolution
gd-graph
solace-mq
jammer
cocoalibspotify-2.0
hidapi
cmmi
diem
vfs
zope.interface
jquery-tools
htdocs
lr
jqote
multiplatform
tmx
svk
resharper-6.0
rowlex
django-piston
echo3
purepdf
movieplayer
appliance
great-circle

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