我正在尝试处理一些数据库操作,比如插入。我能够成功但是当我尝试检索存储的信息时,我得到了这个例外。我已经多次检查了培训视频(在youtube上)并且无法找到出了什么问题
此时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
答案 0 :(得分:1)
此时java.lang.NullPointerException tvresult.setText(data);
所以tvresult
是null
。初始化它,例如在findViewById()
之后使用setContentView()
,传入您布局中的有效视图ID。
您的logcat中看到的CursorIndexOutOfBoundsException
与您发布的此代码版本无关。