New to Android and Java really (I’m a Pascal programmer originally) and having a problem – my app is force closing and I can’t work out why.
I’m simply trying to call an activity to display a table of data from my (currently empty) database. I’m using the ‘Learning Android’ book and have pretty much cut and pasted most of my code out of that. I’ve refactored all my DB access routines to a specific class but seems to be a problem.
Calling method:
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "In onResume...");
// Link to WYWHApplication module
WYWHApplication wywh = (WYWHApplication) this.getApplication();
// Fetch trip list
Cursor tripList = wywh.getBasicList();
and this is the DB class:
public class WYWHApplication extends Application {
private static final String TAG = WYWHApplication.class.getSimpleName();
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "In onCreate for app...");
}
@Override
public void onTerminate() {
super.onTerminate();
Log.i(TAG, "In onTerminate for app...");
}
// Link to DB class
private DatabaseUtils dbUtils;
// Accessor method - how other modules get access to DB.
public DatabaseUtils getDBData() {
return dbUtils;
}
public synchronized Cursor getBasicList() {
Log.i(TAG, "Getting basic list of trips");
// Get list of DB entries
Cursor tripEntries = this.getDBData().fetchList("TRIP_TABLE");
Log.i(TAG, "...returned from getting basic list of trips");
return tripEntries;
}
The LogCat has the following:
10-14 20:38:29.543: INFO/WYWHApplication(305): In onCreate for app...
10-14 20:38:29.603: DEBUG/WYWH(305): Start of onCreate....
10-14 20:38:30.283: DEBUG/WYWH(305): ...end of onCreate.
10-14 20:41:05.943: INFO/ActivityManager(58): Starting activity: Intent { cmp=co.uk.sanetech.wywh/.TripsScreenActivity }
10-14 20:41:06.183: INFO/Trips(305): Start of onCreate....
10-14 20:41:06.773: INFO/Trips(305): In onResume...
10-14 20:41:06.773: INFO/WYWHApplication(305): Getting basic list of trips
10-14 20:41:06.793: DEBUG/AndroidRuntime(305): Shutting down VM
10-14 20:41:06.793: WARN/dalvikvm(305): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-14 20:41:06.993: ERROR/AndroidRuntime(305): FATAL EXCEPTION: main
10-14 20:41:06.993: ERROR/AndroidRuntime(305): java.lang.RuntimeException: Unable to resume activity {co.uk.sanetech.wywh/co.uk.sanetech.wywh.TripsScreenActivity}: java.lang.NullPointerException
10-14 20:41:06.993: ERROR/AndroidRuntime(305): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
(left a bit out here)
10-14 20:41:06.993: ERROR/AndroidRuntime(305): Caused by: java.lang.NullPointerException
10-14 20:41:06.993: ERROR/AndroidRuntime(305): at co.uk.sanetech.wywh.WYWHApplication.getBasicList(WYWHApplication.java:36)
10-14 20:41:06.993: ERROR/AndroidRuntime(305): at co.uk.sanetech.wywh.TripsScreenActivity.onResume(TripsScreenActivity.java:42)
10-14 20:41:06.993: ERROR/AndroidRuntime(305): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
10-14 20:41:06.993: ERROR/AndroidRuntime(305): at android.app.Activity.performResume(Activity.java:3823)
10-14 20:41:06.993: ERROR/AndroidRuntime(305): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
10-14 20:41:06.993: ERROR/AndroidRuntime(305): ... 12 more
Code for fetchList:
public Cursor fetchList(String tabName) {
Log.i(TAG, "In fetchList...");
…as I’m not getting that last debug message, that suggests (to me) that the problem is the call to fetchList but not sure why.
Any help gratefully accepted…
From the code you have posted there, the instance
dbUtilsis never initialized to anything, so it is null. Hence, when you try to callfetchList(), you are calling the method on an instance that doesn’t exist.Your declaration should probably look something like this
Where the object is instantiated before you try to access methods on it.
HTH.