I am trying to create and populate an array of objects with information returned from a database query. I can't seem to get this working properly with my code below.
Any help is appreciated. Thanks in advance.
Module Object:
public class Module {
public String moduleEnglishTitle;
public String moduleJapaneseTitle;
public String moduleCompletionRate;
public int moduleRating;
public Module() {
super();
}
public Module(String moduleEnglishTitle, String moduleJapaneseTitle, String moduleCompletionRate, int moduleRating) {
super();
this.moduleEnglishTitle = moduleEnglishTitle;
this.moduleJapaneseTitle = moduleJapaneseTitle;
this.moduleCompletionRate = moduleCompletionRate;
this.moduleRating = moduleRating;
}
}
These four variables of the object are to be populated from database, into a Module object array in the activity where the data is to be used. This data is then populated into a ListView using a custom ArrayAdapter. The ArrayAdapter, Listview, and Module object all work well together if I manually create the list using this:
Module module_data[] = new Module[] {
new Module("hello","hello","hello", 3),
new Module("hello","hello","hello", 2)
};
However, I don't want to populate it this way, I want to use data from a database using this cursor:
public Cursor getModuleList() {
Cursor mCursor = myDataBase.rawQuery("SELECT mod, engtitle, japtitle FROM master WHERE type='module'", null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
I have this code so far, but it's not working:
Module module_data[] = new Module[moduleInfo.getCount()];
int i = 0;
while(moduleInfo.isAfterLast()) {
module_data[i].moduleJapaneseTitle = moduleInfo.getString(1);
module_data[i].moduleEnglishTitle = moduleInfo.getString(2);
module_data[i].moduleCompletionRate = "済み: " + moduleStats.getString(7);
module_data[i].moduleRating = moduleStats.getInt(4);
moduleInfo.moveToNext();
moduleStats.moveToNext();
i++;
}
.................
06-22 20:42:27.045: E/AndroidRuntime(440): FATAL EXCEPTION: main
06-22 20:42:27.045: E/AndroidRuntime(440): java.lang.RuntimeException: Unable to start activity ComponentInfo{jp.atomicideas.ne/jp.atomicideas.ne.Story2}: java.lang.NullPointerException
06-22 20:42:27.045: E/AndroidRuntime(440): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
06-22 20:42:27.045: E/AndroidRuntime(440): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
06-22 20:42:27.045: E/AndroidRuntime(440): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
06-22 20:42:27.045: E/AndroidRuntime(440): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
06-22 20:42:27.045: E/AndroidRuntime(440): at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 20:42:27.045: E/AndroidRuntime(440): at android.os.Looper.loop(Looper.java:130)
06-22 20:42:27.045: E/AndroidRuntime(440): at android.app.ActivityThread.main(ActivityThread.java:3701)
06-22 20:42:27.045: E/AndroidRuntime(440): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 20:42:27.045: E/AndroidRuntime(440): at java.lang.reflect.Method.invoke(Method.java:507)
06-22 20:42:27.045: E/AndroidRuntime(440): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
06-22 20:42:27.045: E/AndroidRuntime(440): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
06-22 20:42:27.045: E/AndroidRuntime(440): at dalvik.system.NativeStart.main(Native Method)
06-22 20:42:27.045: E/AndroidRuntime(440): Caused by: java.lang.NullPointerException
06-22 20:42:27.045: E/AndroidRuntime(440): at jp.atomicideas.ne.Story2.onCreate(Story2.java:59)
06-22 20:42:27.045: E/AndroidRuntime(440): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-22 20:42:27.045: E/AndroidRuntime(440): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
06-22 20:42:27.045: E/AndroidRuntime(440): ... 11 more