我尝试在Android中插入数据库(SQLite) 但是,当我从我的应用程序发送数据时,出现了这样的错误
10-19 17:19:43.560 19045-19045 / com.example.michael.aplikasi E / AndroidRuntime:FATAL EXCEPTION:main 显示java.lang.NullPointerException 在com.database.DBPelanggan.open(DBPelanggan.java:38) 在com.example.michael.aplikasi.AddPelanggan $ 2.onClick(AddPelanggan.java:101) 在android.view.View.performClick(View.java:4084) 在android.view.View $ PerformClick.run(View.java:16966) 在android.os.Handler.handleCallback(Handler.java:615) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:4797) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 在de.robv.android.xposed.XposedBridge.main(Xpos
这是我的数据库适配器代码
public static final String DATABASE_NAME = "aplikasi_mob_vers";
public static final String TAG = "DBPelanggan";
public static final String DATABASE_TABLE = "pelanggan";
public static final String VAR_KTP = "ktp";
public static final String VAR_NAMA = "nama";
public static final String VAR_ALAMAT = "alamat";
public static final String VAR_IK = "ibu_kandung";
public static final String VAR_BIRTH = "birthdate";
public static final int DATABASE_VERSION = 1;
final Context context;
DatabaseHelper helper;
SQLiteDatabase db;
public DBPelanggan open()
{
db = helper.getWritableDatabase();
return this;
}
public void close()
{
helper.close();
}
public static class DatabaseHelper extends SQLiteOpenHelper
{
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db)
{
try
{
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrade Database dari v." + oldVersion + " ke v." + newVersion);
db.execSQL("DROP table if exist pelanggan");
onCreate(db);
}
public SQLiteDatabase getWritableDatabase()
{
return super.getWritableDatabase();
}
}
public DBPelanggan(Context ctx)
{
this.context = ctx;
}
public boolean deletePelanggan()
{
return db.delete(DATABASE_TABLE, null, null)>0;
}
public void insertPelanggan(Pelanggan pl)
{
ContentValues init = new ContentValues();
init.put(VAR_KTP, pl.ktp); // 0
init.put(VAR_NAMA, pl.nama); // 1
init.put(VAR_ALAMAT, pl.alamat); // 2
init.put(VAR_IK, pl.ibu_kandung); // 3
init.put(VAR_BIRTH, pl.birthdate); // 4
db.insert(DATABASE_TABLE, null, init);
}
public ArrayList<Pelanggan> getDataPelanggan()
{
ArrayList<Pelanggan> listPelanggan = new ArrayList<Pelanggan>();
String selectQuery = "SELECT * FROM pelanggan";
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst())
{
do
{ // ktp nama alamat ibu kandung birth date
Pelanggan pelanggan = new Pelanggan(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
listPelanggan.add(pelanggan);
}
while (cursor.moveToNext());
}
db.close();
return listPelanggan;
}
}
这是我的应用中的代码
Button simpan = (Button) findViewById(R.id.saveButton);
simpan.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Pelanggan pelanggan = new Pelanggan();
pelanggan.nama = et_name.getText().toString();
pelanggan.ktp = et_ktp.getText().toString();
pelanggan.alamat = et_address.getText().toString();
pelanggan.ibu_kandung = et_ibuKandung.getText().toString();
if(pelanggan.nama.equals("") || pelanggan.ktp.equals("") || pelanggan.alamat.equals("") || pelanggan.ibu_kandung.equals("") || day == 0 || month == 0 || year == 0)
{
Toast.makeText(getBaseContext(), "Tolong Isi yang Kosong", Toast.LENGTH_LONG).show();
}
else
{
String birth = year+"-"+month+"-"+day;
pelanggan.birthdate = birth;
Toast.makeText(AddPelanggan.this, "ktp "+pelanggan.ktp+" nama "+pelanggan.nama, Toast.LENGTH_LONG).show();
dbPelanggan = new DBPelanggan(AddPelanggan.this);
dbPelanggan.open();
dbPelanggan.insertPelanggan(pelanggan);
dbPelanggan.close();
}
}
});