【发布时间】:2011-07-15 15:42:47
【问题描述】:
我正在将一些数据输入此数据库,然后尝试显示该数据,但我得到了 Null Pointer Exception at getReadableDatabase()。我输入数据的主要活动如下:
public class TabListData extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.enter);
b.setOnClickListener(this);
Button b1 = (Button) findViewById(R.id.employeeslist);
b1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
EditText name = (EditText) findViewById(R.id.name);
EditText mobile = (EditText) findViewById(R.id.mobile);
String nam = name.getText().toString();
String mob = mobile.getText().toString();
Button b = (Button) v;
Button b1 = (Button) v;
Database d = new Database(getBaseContext());
switch (b.getId()) {
case R.id.enter:
d.input(nam, mob);
case R.id.employeeslist:
/* ArrayList<EmployeesResult> results = d.selectEmployees();
Toast t = Toast.makeText(getBaseContext(), "# records: "
+ results.size(), 1000);
t.show();*/
Intent i = new Intent(this, EmployeesList.class);
// i.putExtra("Name", "Name");
startActivity(i);
}
}
}
这是我的助手类
public class BaseHelper extends SQLiteOpenHelper {
public BaseHelper(Context context) {
super(context, "Employees.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createtable = "create table Employee " + "(Name TEXT, " + "Mobile TEXT)";
db.execSQL(createtable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
这是我的数据库类。我将它用于输入和数据检索。当selectEmployees() 方法不存在但现在显示Null Pointer Exception 时,它可以正常输入。
public class Database {
private BaseHelper helper;
SQLiteDatabase db;
public Database(Context c){
helper = new BaseHelper(c);
}
public void input(String Name, String Mobile){
db = helper.getWritableDatabase();
try{
ContentValues values = new ContentValues();
values.put("Mobile", Mobile);
values.put("Name", Name);
db.insert("Employee", "", values);
Log.e("ENTERED", "ENTERED "+values);
}finally {
if (db != null)
db.close();
}
}
public ArrayList<EmployeesResult> selectEmployees() {
db = helper.getReadableDatabase();
try{
ArrayList<EmployeesResult> results = new ArrayList<EmployeesResult>();
Cursor c = db.rawQuery("select * from Employee", null);
if (c.getCount() > 0) {
c.moveToFirst();
do {
results.add(new EmployeesResult(
c.getString(c.getColumnIndex("Name"))));
//c.getInt(c.getColumnIndex("Mobile"))));
}while(c.moveToNext());
}
return results;
}finally {
if (db != null)
db.close();
}
}
}
EmployeeResult 类:
public class EmployeesResult {
public EmployeesResult(String Name){
name = Name;
//mobile = Mobile;
}
public String name;
public int mobile;
}
这是Logcat
07-15 23:44:11.008: ERROR/AndroidRuntime(292): FATAL EXCEPTION: main
07-15 23:44:11.008: ERROR/AndroidRuntime(292): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.TabListData/com.TabListData.EmployeesList}: java.lang.NullPointerException
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at android.os.Looper.loop(Looper.java:123)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at java.lang.reflect.Method.invoke(Method.java:521)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at dalvik.system.NativeStart.main(Native Method)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): Caused by: java.lang.NullPointerException
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at com.TabListData.Database.selectEmployees(Database.java:35)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at com.TabListData.EmployeesList.onCreate(EmployeesList.java:30)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): ... 11 more
【问题讨论】:
-
代码中的缩进和括号会发生一些非常奇怪的事情。您可能想检查在此处复制时格式是否损坏。
-
另外,哪一行报错了?
-
你为什么不尝试在 BaseHelper 中创建将返回数据库的方法。 db = SQLiteDatabase.openDatabase(pathToTheDatabase, null, SQLiteDatabase.OPEN_READWRITE);假设数据库已经存在于您的 /data/data/
/databases 中。或使用 SQLiteDatabase.openOrCreateDatabase(file.getPath(), null);这相当于前面提到的方式。您的数据库为空,这就是您抛出 NullPointerException 的原因 -
我也尝试在 DaseHelper 中创建方法,但结果相同 Null 指针异常我已经检查了我的数据库 在创建 selectEmployees() 之前我输入的不是空条目我正在发布 Logcat 请帮助.
标签: android sqlite nullpointerexception