1

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
4

5 回答 5

0

加一个感叹号moduleInfo.isAfterLast()

于 2012-06-22T11:00:54.650 回答
0

你这里有问题while(moduleInfo.isAfterLast())

将您的代码更改为以下代码段。

int i = 0;
if (moduleInfo.moveToFirst() && moduleStats.moveToFirst())
{
  do {

    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);

    moduleStats.moveToNext();
    i++;
  } while (moduleInfo.moveToNext());
}
于 2012-06-22T10:57:28.160 回答
0

利用

 module_data[i] = new Module ();

“!” 不见了..........

 while(!moduleInfo.isAfterLast()) {
        module_data[i] = new Module ();//<--------------------------
        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++;
    }
于 2012-06-22T10:59:01.497 回答
0

我能够通过这种方式解决它:

    Module module_data[] = new Module[moduleInfo.getCount()];
    int i;
    for(i=0; i<7; i++) {
        module_data[i] = new Module(moduleInfo.getString(1), moduleInfo.getString(2),  "済み: " + moduleStats.getString(7), moduleStats.getInt(4));
        moduleInfo.moveToNext();
        moduleStats.movetoNext();
    }

问题是,我通过其构造函数错误地创建和使用了 Module 对象。

感谢大家的帮助!

于 2012-06-27T02:24:58.553 回答
0

试试这个,我正在使用ArrayList 代替......

Module mod;

ArrayList<Module> module_data = new ArrayList<Module>();

final Cursor cursor = db.query(DbHelper.TABLE_NAME, null, null, null, null, null, null,null );

while(cursor.moveToNext()){

mod = new Module();

module_data.add(new module(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getInt(3));

}

于 2012-06-22T12:38:44.683 回答