我只是想在我的Android应用程序中建立与数据库的连接并在其中插入一些值,但我得到了一个运行时异常:
06-12 01:02:47.847: E/AndroidRuntime(27848): FATAL EXCEPTION: main
06-12 01:02:47.847: E/AndroidRuntime(27848): Process: com.example.database,PID: 27848
06-12 01:02:47.847: E/AndroidRuntime(27848): java.lang.NullPointerException
06-12 01:02:47.847: E/AndroidRuntime(27848): at com.example.database.DataBaseManager.onCreate(DataBaseManager.java:38)
06-12 01:02:47.847: E/AndroidRuntime(27848): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
06-12 01:02:47.847: E/AndroidRuntime(27848): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
06-12 01:02:47.847: E/AndroidRuntime(27848): at com.example.database.MainActivity$1.onClick(MainActivity.java:31)
06-12 01:02:47.847: E/AndroidRuntime(27848): at android.view.View.performClick(View.java:4640)
06-12 01:02:47.847: E/AndroidRuntime(27848): at android.view.View$PerformClick.run(View.java:19421)
06-12 01:02:47.847: E/AndroidRuntime(27848): at android.os.Handler.handleCallback(Handler.java:733)
06-12 01:02:47.847: E/AndroidRuntime(27848): at android.os.Handler.dispatchMessage(Handler.java:95)
06-12 01:02:47.847: E/AndroidRuntime(27848): at android.os.Looper.loop(Looper.java:146)
06-12 01:02:47.847: E/AndroidRuntime(27848): at android.app.ActivityThread.main(ActivityThread.java:5602)
06-12 01:02:47.847: E/AndroidRuntime(27848): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 01:02:47.847: E/AndroidRuntime(27848): at java.lang.reflect.Method.invoke(Method.java:515)
06-12 01:02:47.847: E/AndroidRuntime(27848): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
06-12 01:02:47.847: E/AndroidRuntime(27848): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
的DatabaseManager:
public class DataBaseManager extends SQLiteOpenHelper {
public static String DATABASE_NAME="First DB";
public static String TABLE_NAME="First table";
public static String COL_FNAME="fname";
public static String COL_LNAME="lname";
public static final String COLUMN_ID = "_id";
private static final String DATABASE_CREATE = "create table First DB"+ "(" + COLUMN_ID + " integer primary key autoincrement, " + COL_FNAME+ " text ,"+COL_LNAME+"text);";
SQLiteDatabase db;
private static final String DATABASE_CREATE = “create table “ DATABASE_TABLE+ “ (“ + KEY_ID +“ integer primary key autoincrement, “ +KEY_NAME + “ text not null);”;*/
public DataBaseManager(Context context) {
super(context, DATABASE_NAME, null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
db.execSQL("create table First DB " +
"(id integer primary key, fname text,lname text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+DATABASE_NAME);
onCreate(db);
}
public void insert(SQLiteDatabase ddb,String fname,String lname)
{
ddb.execSQL("insert into " +DATABASE_NAME+"values(null,'"+fname+"','"+lname+"');");
}
public void readData() {
// TODO Auto-generated method stub
SQLiteDatabase db=this.getReadableDatabase();
Cursor res=db.rawQuery("select * from"+DATABASE_NAME+";",null);
int rows=res.getCount();
Toast.makeText(null,"now of rows are "+rows+"tillnow", Toast.LENGTH_LONG).show();
}
MainActivity:
public class MainActivity extends ActionBarActivity {
Button insert,read;
DataBaseManager dbm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
insert=(Button)findViewById(R.id.insert);
read=(Button)findViewById(R.id.read);
dbm=new DataBaseManager(this);
//getWritableDatabase();
insert.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SQLiteDatabase db=
dbm.getWritableDatabase();
dbm.insert(db,"nikhil", "saxena");
dbm.insert(db,"rahul", "saxena");
dbm.insert(db,"rahul", "dhillon");
dbm.insert(db,"nikhil", "dhillon");
}
});
请建议。
答案 0 :(得分:1)
将arg0更改为db。
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table First DB " +
"(id integer primary key, fname text,lname text)"
);
}