gpt4 book ai didi

java - IllegalArgumentException Android 中的 SQLite 数据库绑定(bind)值索引 1 为空

转载 作者:行者123 更新时间:2023-11-30 02:49:11 26 4
gpt4 key购买 nike

我想在 ListView 中显示 SQLite 数据库中所有列的平均值。我在查询中使用 average 函数和 group by 子句,但是当我运行该应用程序时,我的应用程序崩溃并出现 IllegalArgumentException 并且绑定(bind)值索引 1 为空。

我不知道我的错误在哪里,甚至我没有在 SQLite db 中插入任何空值。所以可以帮助我一些。提前致谢。

这是我的代码:

private void showPerformanceDetails()
{
ArrayList<Performance_Pojo> Performance_PojoList = new ArrayList<Performance_Pojo>();
Performance_PojoList.clear();

SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase();

Cursor cursor = sqlDatabase.rawQuery("SELECT performance_month, AVG(performance_rate_one), AVG(performance_rate_two), AVG(performance_rate_three), AVG(performance_rate_four), AVG(performance_rate_five) FROM performance where "+ "Emp_id" + " = ? "
+" GROUP BY performance_month",new String[]{strSeparated_Id});


if (cursor != null && cursor.getCount() != 0)
{
if (cursor.moveToFirst())
{
do
{
Performance_Pojo Performance_PojoListItems = new Performance_Pojo();

Performance_PojoListItems.set_strPerformanceMonth(cursor.getString(cursor.getColumnIndex("performance_month")));
Performance_PojoListItems.set_strPerformance_rate_one(cursor.getString(cursor.getColumnIndex("performance_rate_one")));
Performance_PojoListItems.set_strPerformance_rate_two(cursor.getString(cursor.getColumnIndex("performance_rate_two")));
Performance_PojoListItems.set_strPerformance_rate_three(cursor.getString(cursor.getColumnIndex("performance_rate_three")));
Performance_PojoListItems.set_strPerformance_rate_four(cursor.getString(cursor.getColumnIndex("performance_rate_four")));
Performance_PojoListItems.set_strPerformance_rate_five(cursor.getString(cursor.getColumnIndex("performance_rate_five")));

Performance_PojoList.add(Performance_PojoListItems);

}while (cursor.moveToNext());
}

db.close();
cursor.close();

}


PerformanceList_Adapter performanceList_Adapter = new PerformanceList_Adapter(Performance_Details.this, Performance_PojoList);
list_PerformanceDetails.setAdapter(performanceList_Adapter);

}

这是我的 Log cat 错误:

07-01 12:15:15.366: E/AndroidRuntime(14850): FATAL EXCEPTION: main
07-01 12:15:15.366: E/AndroidRuntime(14850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sqlitedemo/com.sqlitedemo.Performance_Details}: java.lang.IllegalArgumentException: the bind value at index 1 is null
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.os.Looper.loop(Looper.java:123)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-01 12:15:15.366: E/AndroidRuntime(14850): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 12:15:15.366: E/AndroidRuntime(14850): at java.lang.reflect.Method.invoke(Method.java:507)
07-01 12:15:15.366: E/AndroidRuntime(14850): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-01 12:15:15.366: E/AndroidRuntime(14850): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-01 12:15:15.366: E/AndroidRuntime(14850): at dalvik.system.NativeStart.main(Native Method)
07-01 12:15:15.366: E/AndroidRuntime(14850): Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:237)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.database.sqlite.SQLiteQuery.bindString(SQLiteQuery.java:185)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:48)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)
07-01 12:15:15.366: E/AndroidRuntime(14850): at com.sqlitedemo.Performance_Details.showPerformanceDetails(Performance_Details.java:79)
07-01 12:15:15.366: E/AndroidRuntime(14850): at com.sqlitedemo.Performance_Details.onCreate(Performance_Details.java:63)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

最佳答案

“绑定(bind)值”显然指的是您选择到 rawQuery() 中的选择的 selectionArgs。如果这样的 selArgs 值为 null,您会得到这个。

编辑:

    Cursor cursor = sqlDatabase.rawQuery("SELECT performance_month, AVG(performance_rate_one),  AVG(performance_rate_two),  AVG(performance_rate_three),  AVG(performance_rate_four),  AVG(performance_rate_five)  FROM performance where "+ "Emp_id" + " = ? " 
+" GROUP BY performance_month",new String[]{strSeparated_Id});

所有参数位置都应该用?指定。尽管如此,查询的参数始终是字符串,因此整数没有什么特别之处。这应该适合你:

改变:

new String[]{strSeparated_Id}

new String[]{String.valueOf(strSeparated_Id)}

关于java - IllegalArgumentException Android 中的 SQLite 数据库绑定(bind)值索引 1 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24504213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com