When I insert data into sqlite database in the logcat it prompt up the null object reference. But with the same code i using in another program it can work. Can i have some suggestion to solve this question.
this is my DBControl
public void addProduct(Product product) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(Code,product.getProductCode());
cv.put(Description,product.getProductName());
cv.put(MenuDepartment,product.getMenuDepartment());
cv.put(CondimentGroup,product.getCondimentGroup1());
cv.put(ModifierGroup,product.getModifierGroup1());
//inserting row
db.insert(mproduct, null, cv);
//close the database to avoid any leak
db.close();
}
this is how i insert data when button click.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_printer_test_demo);
innitView();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_printer_test_demo);
innitView();
}
private void innitView() {
new BitmapUtils(this);
b_table = (Button) findViewById(R.id.b_table);
b_additem = (Button)findViewById(R.id.additem);
findViewById(R.id.additem).setOnClickListener(this);
findViewById(R.id.b_exit).setOnClickListener(this);
b_additem.setOnClickListener(this);
b_table.setOnClickListener(this);
info = (TextView) findViewById(R.id.info);
callback = new ICallback.Stub() {
@Override
public void onRunResult(final boolean success) throws RemoteException {
}
@Override
public void onReturnString(final String value) throws RemoteException {
Log.i(TAG,"printlength:" + value + "\n");
}
@Override
public void onRaiseException(int code, final String msg) throws RemoteException {
Log.i(TAG,"onRaiseException: " + msg);
runOnUiThread(new Runnable(){
@Override
public void run() {
info.append("onRaiseException = " + msg + "\n");
}});
}
};
setButtonEnable(false);
Intent intent=new Intent();
intent.setPackage("woyou.aidlservice.jiuiv5");
intent.setAction("woyou.aidlservice.jiuiv5.IWoyouService");
startService(intent);
bindService(intent, connService, Context.BIND_AUTO_CREATE);
}
private void setButtonEnable(boolean flag){
b_table.setEnabled(flag);
b_additem.setEnabled(flag);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.b_table:
//打印表格
printTable();
break;
case R.id.additem:
additem();
break;
}
}
public void additem(){
try {
DBControl db = new DBControl(context);<------the logcat show the error at here
db.addProduct(new Product("10001", "Nasi Lemak", "A", "C1", "M1"));
db.addProduct(new Product("10002", "STEAM LOTUS LEAF GLUTINOUS RICE", "A", "C1", "M1"));
db.addProduct(new Product("10003", "EIGHT TREASURE GLUTINOUS RICE", "B", "C2", "M2"));
db.addProduct(new Product("10004", "CHICKEN PORRIDGE & DRIED SCALLOP", "B", "C2", "M2"));
db.addProduct(new Product("10005", "FISH PORRIDGE", "C", "C3", "M3"));
db.addProduct(new Product("10006", "LAKSA", "C", "C3", "M3"));
}catch(SQLiteException e){
e.printStackTrace();
}
}
logcat
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.test.printertestdemo.DBControl.addProduct(com.test.printertestdemo.Constructor.Product)' on a null object reference
at com.test.printertestdemo.PrinterTestDemoAct.additem(PrinterTestDemoAct.java:181)
at com.test.printertestdemo.PrinterTestDemoAct.onClick(PrinterTestDemoAct.java:173)
at android.view.View.performClick(View.java:4848)
at android.view.View$PerformClick.run(View.java:20262)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)