开发者

Null pointer after sorted database query

I am trying to query my database and sort my start column in descending order then get the first row.

public Cursor getLowestEvent(){
    String sort = "SELECT * FROM " + EVENTS_TABLE + " ORDER_BY " + START + " DESC";

    Cursor c = db.query(EVENTS_TABLE, new String[] {ID,START},null,null,null,null,sort); //error
    if(c != null){
        c.moveToFirst();
    }
    return c;
}

doing this gives me a null pointer exception and I dont know why, there are items in the database so it has to be a problem with my sort?

08-25 11:03:14.503: ERROR/AndroidRuntime(2367): java.lang.NullPointerException
08-25 11:03:14.503: ERROR/AndroidRuntime(2367):     at com.app.notifyme.CalendarDB.getLowestEvent(CalendarDB.java:106)
08-25 11:03:14.503: ERROR/AndroidRuntime(2367):     at com.app.notifyme.Calendar$5.onClick(Calendar.java:162)
08-25 11:03:14.503: ERROR/AndroidRuntime(2367):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:873)
08-25 11:03:14.503: ERROR/AndroidRuntime(2367):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
08-25 11:03:14.503: ERROR/AndroidRuntime(2367):     at android.widget.ListView.performItemClick(ListView.java:3513)
08-25 11:03:14.503: ERROR/AndroidRuntime(2367):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1849)
08-25 11:03:14.503: ERROR/AndroidRuntime(2367):     at android.os.Handler.handleCallback(Handler.java:587)
08-25 11:03:14.503: ERROR/AndroidRuntime(2367):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-25 11:03:14.503: ERROR/AndroidRuntime(2367):     at android.os.Looper.loop(Looper.java:123)
08-25 11:03:14.503: ERROR/AndroidRuntime(2367):     at android.app.ActivityThread.main(ActivityThread.java:3839)
08-25 11:03:14.503: E开发者_运维知识库RROR/AndroidRuntime(2367):     at java.lang.reflect.Method.invokeNative(Native Method)
08-25 11:03:14.503: ERROR/AndroidRuntime(2367):     at java.lang.reflect.Method.invoke(Method.java:507)
08-25 11:03:14.503: ERROR/AndroidRuntime(2367):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-25 11:03:14.503: ERROR/AndroidRuntime(2367):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-25 11:03:14.503: ERROR/AndroidRuntime(2367):     at dalvik.system.NativeStart.main(Native Method)


Please see this link. Your last parameter (order by) is not supposed to be an ENTIRE sql statement (SELECT * FROM ...). It is supposed to be formatted as a SQL ORDER BY clause. Since it says exclude the ORDER BY itself, I think it would just be the column you want to order by and the identifier to determine which way to order ("ID DESC").

How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.

The way you are building your query (in the string "sort"), you could also execute db.rawQuery.

EDIT: What resolved the issue was adding db.open() before the db.query() call.


I think following will work make sure that 1] Your database is created. 2] Table is exist. 3] Field what you are mentioned are exist.

Cursor c = db.query(EVENTS_TABLE, new String[] {ID,START},null,null,null,null,START + " DESC")


You are passing in a complete SELECT ... statement to db.query(). It looks like you should be using db.rawQuery() instead. Read the documentation for these functions to check what arguments they expect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜