2015-03-13 96 views
-1

我正在嘗試爲我的活動實現executeUpdate查詢。但是我得到這個錯誤:SQLite執行更新錯誤

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.techfrk.fetchinboxsms.Database_CustomTransaction.executeUpdate(java.lang.String)' on a null object reference 

這裏是我的代碼:

public class AddTransaction extends Activity implements OnItemSelectedListener, View.OnClickListener, OnItemClickListener 
{ 
    DateFormat formate = DateFormat.getDateInstance(); 
    Calendar calendar = Calendar.getInstance(); 

    ImageButton imgbtn_personal,imgbtn_debitCredit,imgbtn_transferAnother,imgbtn_incomeExpense,calendarimg; 

    Button dialogButton; 

    Database_CustomTransaction mydb1; 

    Boolean personal_flag=true, debitCredit_flag=true, transferAccount=true, incomeExpense=true; 

    String transaction_type = "No type selected !"; 
    public static final String list_prefs = "PreferencesFile"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_add_transaction); 
     imgbtn_personal = (ImageButton) findViewById(R.id.personal_business); 
     imgbtn_debitCredit = (ImageButton) findViewById(R.id.debit_credit_refund); 
     imgbtn_transferAnother = (ImageButton) findViewById(R.id.transfer_another_acc); 
     imgbtn_incomeExpense = (ImageButton) findViewById(R.id.income_expense); 

     calendarimg = (ImageButton) findViewById(R.id.calendarbtn); 
     calendarimg.setOnClickListener(this);     //ERROR IN THIS LINE 
     updatedate(); 

     dialogButton = (Button) findViewById(R.id.addentry); 
     // if button is clicked, close the custom dialog 
     dialogButton.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       EditText spent = (EditText) findViewById(R.id.spent_on); 
       EditText amt = (EditText) findViewById(R.id.amt_insert); 
       EditText datee = (EditText) findViewById(R.id.alertdate); 

       System.out.println("VALUE 1 :" +transaction_type); 
       System.out.println("VALUE 2 :" +amt.getText()); 
       System.out.println("VALUE 3 :" +spent.getText().toString()); 
       System.out.println("VALUE 4 :" +datee.getText().toString()); 
           mydb1.executeUpdate("Insert into AirtelMoneyCustom (type, amount, spentOn, date) values ('"+transaction_type+"'" + "," + "'"+ amt.getText()+ "'"+ ","+ "'"+ spent.getText().toString()+ "'"+ ","+ "'"+ datee.getText().toString()+ "'"+ ")"); 

        Context context = getApplicationContext(); 
        CharSequence text = " Transaction recorded "; 
        int duration = Toast.LENGTH_SHORT; 

        mydb1=new Database_CustomTransaction(getApplicationContext()); 
        Toast toast = Toast.makeText(context, text, duration); 
        toast.show(); 

        startActivity(new Intent(AddTransaction.this, AirtelMoney.class)); 
//    } 

      } 
     }); 

     imgbtn_personal.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       if(personal_flag==true) 
       { 
        imgbtn_personal.setImageResource(R.drawable.personal_business_transaction_invert); 
        transaction_type = "Personal/Business"; 
        personal_flag=false; 
       } 
       else 
       { 
        imgbtn_personal.setImageResource(R.drawable.personal_business_transaction); 
        transaction_type = "No type selected !"; 
        personal_flag=true; 
       } 

      } 
     }); 
     imgbtn_debitCredit.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       if(debitCredit_flag==true) 
       { 
        imgbtn_debitCredit.setImageResource(R.drawable.debit_credit_refund_transaction_invert); 
        transaction_type = "Debit/Credit/Refund"; 
        debitCredit_flag=false; 
       } 
       else 
       { 
        imgbtn_debitCredit.setImageResource(R.drawable.debit_credit_refund_transaction); 
        transaction_type = "No type selected !"; 
        debitCredit_flag=true; 
       } 

      } 
     }); 
     imgbtn_transferAnother.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       if(transferAccount==true) 
       { 
        imgbtn_transferAnother.setImageResource(R.drawable.transfer_another_acc_transaction_invert); 
        transaction_type = "Account Transfer"; 
        transferAccount=false; 
       } 
       else 
       { 
        imgbtn_transferAnother.setImageResource(R.drawable.transfer_another_acc_transaction); 
        transaction_type = "No type selected !"; 
        transferAccount=true; 
       } 

      } 
     }); 
     imgbtn_incomeExpense.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       if(incomeExpense==true) 
       { 
        imgbtn_incomeExpense.setImageResource(R.drawable.income_expense_transaction_invert); 
        transaction_type = "Income/Expense"; 
        incomeExpense=false; 
       } 
       else 
       { 
        imgbtn_incomeExpense.setImageResource(R.drawable.income_expense_transaction); 
        transaction_type = "No type selected !"; 
        incomeExpense=true; 
       } 

      } 
     }); 


    } 
+0

的可能重複[什麼是空指針異常,以及如何解決?(http://stackoverflow.com/questions/218384/ what-is-null-pointer-exception-and-how-do-i-fix-it) – 2015-09-10 14:10:34

回答

6

mydb1爲null,因爲你使用它:

mydb1.executeUpdate() 

,然後初始化:

mydb1=new Database_CustomTransaction(getApplicationContext()); 
+0

Ohhhh !! Yaaa ...對不起......我的壞! – TechFrk 2015-03-13 10:21:44

2

您應該initialize您在mydb1ObjectonCreate(...)

mydb1=new Database_CustomTransaction(getApplicationContext()); 

updatedate();