2014-03-13 77 views
0

我想處理一些數據庫操作喜歡插入。我能夠成功但當我嘗試檢索存儲的信息我得到這個異常。我已經檢查了幾次培訓視頻(上youtube),並不能找出什麼是錯的安卓NullPointerException異常檢索數據

java.lang.NullPointerException此時tvresult.setText(data);

public static final String KEY_ROWID="id"; 
public static final String KEY_NAME="u_name"; 
public static final String KEY_PASS="u_pass"; 


btnshow.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       try { 
        db.Open(); 
        String data=db.getThat(); 
        tvresult.setText(data); 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       db.Close(); 

      } 
     }); 




public String getThat() { 
     // TODO Auto-generated method stub 

    String columns[]={KEY_ROWID,KEY_NAME,KEY_PASS}; 

    Cursor c=ourdatabase.query(DB_TABLE,columns,null,null,null,null,null); 

    int iRow=c.getColumnIndex(KEY_ROWID); 
    int iName=c.getColumnIndex(KEY_NAME); 
    int iPass=c.getColumnIndex(KEY_PASS); 

    String result=""; 


    for (c.moveToFirst();!c.isAfterLast();c.moveToNext()) { 
     result=result+"Name :"+c.getString(iName)+"Pass :"+c.getString(iPass); 

    } 


return result; 
    } 

logcat的:

30: E/AndroidRuntime(1711): FATAL EXCEPTION: main 
03-13 02:59:49.630: E/AndroidRuntime(1711): Process: com.example.sqllite, PID: 1711 
03-13 02:59:49.630: E/AndroidRuntime(1711): java.lang.NullPointerException 
03-13 02:59:49.630: E/AndroidRuntime(1711):  at com.example.sqllite.Db$1.onClick(Db.java:33) 
03-13 02:59:49.630: E/AndroidRuntime(1711):  at android.view.View.performClick(View.java:4424) 
03-13 02:59:49.630: E/AndroidRuntime(1711):  at android.view.View$PerformClick.run(View.java:18383) 
03-13 02:59:49.630: E/AndroidRuntime(1711):  at android.os.Handler.handleCallback(Handler.java:733) 
03-13 02:59:49.630: E/AndroidRuntime(1711):  at android.os.Handler.dispatchMessage(Handler.java:95) 
03-13 02:59:49.630: E/AndroidRuntime(1711):  at android.os.Looper.loop(Looper.java:137) 
03-13 02:59:49.630: E/AndroidRuntime(1711):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
03-13 02:59:49.630: E/AndroidRuntime(1711):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-13 02:59:49.630: E/AndroidRuntime(1711):  at java.lang.reflect.Method.invoke(Method.java:515) 
03-13 02:59:49.630: E/AndroidRuntime(1711):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
03-13 02:59:49.630: E/AndroidRuntime(1711):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
03-13 02:59:49.630: E/AndroidRuntime(1711):  at dalvik.system.NativeStart.main(Native Method) 
03-13 02:59:55.470: I/Choreographer(1824): Skipped 160 frames! The application may be doing too much work on its main thread. 
03-13 03:10:48.065: W/EGL_emulation(1824): eglSurfaceAttrib not implemented 
03-13 03:10:51.845: D/dalvikvm(1887): GC_FOR_ALLOC freed 53K, 4% free 3275K/3384K, paused 2ms, total 6ms 
03-13 03:10:51.945: W/EGL_emulation(1887): eglSurfaceAttrib not implemented 
03-13 03:10:53.665: D/AndroidRuntime(1887): Shutting down VM 
03-13 03:10:53.665: W/dalvikvm(1887): threadid=1: thread exiting with uncaught exception (group=0xb0d51b08) 
03-13 03:10:53.665: E/AndroidRuntime(1887): FATAL EXCEPTION: main 
03-13 03:10:53.665: E/AndroidRuntime(1887): Process: com.example.sqllite, PID: 1887 
03-13 03:10:53.665: E/AndroidRuntime(1887): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 5 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426) 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at com.example.sqllite.Database.getThat(Database.java:116) 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at com.example.sqllite.Db$1.onClick(Db.java:32) 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at android.view.View.performClick(View.java:4424) 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at android.view.View$PerformClick.run(View.java:18383) 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at android.os.Handler.handleCallback(Handler.java:733) 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at android.os.Handler.dispatchMessage(Handler.java:95) 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at android.os.Looper.loop(Looper.java:137) 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at java.lang.reflect.Method.invoke(Method.java:515) 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
03-13 03:10:53.665: E/AndroidRuntime(1887):  at dalvik.system.NativeStart.main(Native Method) 
03-13 03:10:55.675: D/(1906): HostConnection::get() New Host Connection established 0xb8204250, tid 1906 
+2

發佈您的logcat錯誤跟蹤。 –

+0

你能把你的日誌貓錯誤信息 –

+0

完整的日誌貓請 – Riskhan

回答

1

顯示java.lang.NullPointerException此時tvresult.setText(數據);

因此tvresultnull。初始化它,例如在setContentView()之後findViewById(),傳入佈局中的有效視圖ID。

您的logcat中看到的CursorIndexOutOfBoundsException與您發佈的此版本代碼無關。

+0

CursorIndexOutOfBoundsException確實與這部分相關的代碼,因爲他的投影中沒有BaseColumns._ID列 –

+0

@ LeMoN.xaH這隻會是遊標適配器的問題,而OP沒有它。爲什麼它沒有關聯是因爲它是來自'getString()'列訪問,光標中有5列,OP的代碼只有3個。 – laalto

+0

現在感覺像是一個ididot。感謝你。夥計。但是,哪一行表明你怎麼知道 – user2583511

相關問題