SQLite搜索:空指针异常

时间:2013-02-08 12:06:21

标签: android database search nullpointerexception

我在sqlite数据库中搜索时收到空指针异常

    LinkedList<String> results = databasehelper_obj.search(str_list_heading);

                if(results.isEmpty()){

                    System.out.println(" inside if");
                    }else
                    {
                        System.out.println(" inside else");
                      } 

这是我尝试调用搜索方法的部分,我的搜索方法是另一个类定义为 -

     public LinkedList<String> search(String search) {

    LinkedList<String> results = new LinkedList<String>();
    Cursor cursor = null;
    try{
        cursor = this.sql_liteDatabase_obj.query(true, LIST_CREATE, new String[] { LIST_HEADING }, LIST_CREATE + " MATCH ?", new String[] { search }, null, null, null, null);

        if(cursor!=null && cursor.getCount()>0 && cursor.moveToFirst()){
            int iUsername = cursor.getColumnIndex(LIST_HEADING);


            do{
                results.add(
                    new String(
                        "Username: "+cursor.getString(iUsername) 

                    )
                );
                System.out.println(" control reach to result"+iUsername);
            }while(cursor.moveToNext());
        }
    }catch(Exception e){
        Log.e(APP_NAME, "An error occurred while searching for "+search+": "+e.toString(), e);
    }finally{
        if(cursor!=null && !cursor.isClosed()){
            cursor.close();
        }
    }

    return results;
}

我的日志猫输出如下 -

                                        02-08 12:04:01.845: I/System.out(25206): inside submit button
                     02-08 12:04:01.875: I/System.out(25206): list heading Food
                     02-08 12:04:01.875: D/AndroidRuntime(25206): Shutting down VM
                     02-08 12:04:01.875: W/dalvikvm(25206): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
                    02-08 12:04:01.895: E/AndroidRuntime(25206): FATAL EXCEPTION: main
                     02-08 12:04:01.895: E/AndroidRuntime(25206): java.lang.NullPointerException
                     02-08 12:04:01.895: E/AndroidRuntime(25206):   at com.kt.listmanagement.createlist.CreateList$2.onClick(CreateList.java:93)
                   02-08 12:04:01.895: E/AndroidRuntime(25206):     at android.view.View.performClick(View.java:4084)
                   02-08 12:04:01.895: E/AndroidRuntime(25206):     at android.view.View$PerformClick.run(View.java:16966)
                    02-08 12:04:01.895: E/AndroidRuntime(25206):    at android.os.Handler.handleCallback(Handler.java:615)
                    02-08 12:04:01.895: E/AndroidRuntime(25206):    at android.os.Handler.dispatchMessage(Handler.java:92)
                     02-08 12:04:01.895: E/AndroidRuntime(25206):   at android.os.Looper.loop(Looper.java:137)
                    02-08 12:04:01.895: E/AndroidRuntime(25206):    at android.app.ActivityThread.main(ActivityThread.java:4745)
                   02-08 12:04:01.895: E/AndroidRuntime(25206):     at java.lang.reflect.Method.invokeNative(Native Method)
                    02-08 12:04:01.895: E/AndroidRuntime(25206):    at java.lang.reflect.Method.invoke(Method.java:511)
                02-08 12:04:01.895: E/AndroidRuntime(25206):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                02-08 12:04:01.895: E/AndroidRuntime(25206):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                     02-08 12:04:01.895: E/AndroidRuntime(25206):   at dalvik.system.NativeStart.main(Native Method)

我在com.kt.listmanagement.createlist.CreateList $ 2.onClick(CreateList.java:93)的行是 如下 - (我已经在上面提到过)

                         LinkedList<String> results = databasehelper_obj.search(str_list_heading);

0 个答案:

没有答案