2

I want to display average of all the column from SQLite database in listview . I'm using average function and group by clause in the query but when i run the app my app is crashing and getting IllegalArgumentException and the bind value index 1 is null.

I don't know where is my mistake and even i'm not inserted any null value in SQLite db.SO can help me some one. Thanks in advance.

Here is my code:

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

    }

Here is my Log cat error:

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)
2
  • it seems strSeparated_Id is coming as null. print the value in log and check Commented Jul 1, 2014 at 7:10
  • @Sanjeev : I have to check my log and I'm getting value in strSeparated_Id. Commented Jul 1, 2014 at 7:14

1 Answer 1

2

"The bind value" apparently refers to the selectionArgs for the selection, that you select into rawQuery(). If such a selArgs value is null, you get this.

Edit:

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

All parameter places should be designated with ?. The parameters to the query are always string nontheless, so nothing special about the integers. This should work for you:

Change:

new String[]{strSeparated_Id}

to

new String[]{String.valueOf(strSeparated_Id)}
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry but i didn't get you.
@ TeRRo : But i'm getting value strSeparated_Id = 1 so how will null ?
@ TeRRo:I have done all steps what you have said but query showing no records.
@Robotics But you do not get errors? Are you sure you have inserted data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.