空指针异常代码:
ArrayList<Item> m_parts = new ArrayList<Item>();
SQLiteDatabase db ;
try{
db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from expenses", null );
res.moveToFirst();
String amt,typ,rs,dt,tp,p2,p1,p3;
int amount;
while(res.isAfterLast() == false){
rs=res.getString(res.getColumnIndex(EXPENSES_COLUMN_AMOUNT));
tp=res.getString(res.getColumnIndex(EXPENSES_COLUMN_TYPE));
dt=res.getString(res.getColumnIndex(EXPENSES_COLUMN_DATE));
amount=Integer.parseInt(rs);
m_parts.add(new Item("hello","there", 10));
}
}
catch(Exception e)
{
System.out.println("new Error :"+e);
}
return m_parts;
日志文件
08-11 13:45:43.041:D / gralloc_goldfish(2448):未检测到GPU仿真的仿真器。 08-11 13:45:46.242:D / AndroidRuntime(2448):关闭VM 08-11 13:45:46.313:W / dalvikvm(2448):threadid = 1:线程退出时未捕获异常(组= 0x414c4700) 08-11 13:45:46.412:E / AndroidRuntime(2448):致命异常:主要 08-11 13:45:46.412:E / AndroidRuntime(2448):java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.myexpenses / com.example.myexpenses.ListViewMainActivity}:java.lang.NullPointerException 08-11 13:45:46.412:E / AndroidRuntime(2448):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137) 08-11 13:45:46.412:E / AndroidRuntime(2448):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 08-11 13:45:46.412:E / AndroidRuntime(2448):在android.app.ActivityThread.access $ 600(ActivityThread.java:141) 08-11 13:45:46.412:E / AndroidRuntime(2448):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256) 08-11 13:45:46.412:E / AndroidRuntime(2448):在android.os.Handler.dispatchMessage(Handler.java:99) 08-11 13:45:46.412:E / AndroidRuntime(2448):在android.os.Looper.loop(Looper.java:137) 08-11 13:45:46.412:E / AndroidRuntime(2448):在android.app.ActivityThread.main(ActivityThread.java:5103) 08-11 13:45:46.412:E / AndroidRuntime(2448):at java.lang.reflect.Method.invokeNative(Native Method) 08-11 13:45:46.412:E / AndroidRuntime(2448):at java.lang.reflect.Method.invoke(Method.java:525) 08-11 13:45:46.412:E / AndroidRuntime(2448):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737) 08-11 13:45:46.412:E / AndroidRuntime(2448):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-11 13:45:46.412:E / AndroidRuntime(2448):at dalvik.system.NativeStart.main(Native Method) 08-11 13:45:46.412:E / AndroidRuntime(2448):引起:java.lang.NullPointerException 08-11 13:45:46.412:E / AndroidRuntime(2448):在android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:235) 08-11 13:45:46.412:E / AndroidRuntime(2448):在android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 08-11 13:45:46.412:E / AndroidRuntime(2448):在android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188) 08-11 13:45:46.412:E / AndroidRuntime(2448):at com.example.myexpenses.Dbhelper.gettoday1(Dbhelper.java:253) 08-11 13:45:46.412:E / AndroidRuntime(2448):at com.example.myexpenses.ListViewMainActivity。(ListViewMainActivity.java:18) 08-11 13:45:46.412:E / AndroidRuntime(2448):at java.lang.Class.newInstanceImpl(Native Method) 08-11 13:45:46.412:E / AndroidRuntime(2448):at java.lang.Class.newInstance(Class.java:1130) 08-11 13:45:46.412:E / AndroidRuntime(2448):在android.app.Instrumentation.newActivity(Instrumentation.java:1061) 08-11 13:45:46.412:E / AndroidRuntime(2448):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128) 08-11 13:45:46.412:E / AndroidRuntime(2448):... 11更多
答案 0 :(得分:0)
传递给Context
构造函数的SQLiteOpenHelper
不是有效的Context
,其基本上下文是null
。
这可能是由于您使用Activity
new
作为Context
传递了new
。切勿使用{{1}}实例化活动。