-1

I am trying to implement executeUpdate query for my activity. However I am getting this error :

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

Here is my code :

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;
                }

            }
        });


    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
TechFrk
  • 185
  • 2
  • 2
  • 15
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – ci_ Sep 10 '15 at 14:10

2 Answers2

6

mydb1 is null because you're using it:

mydb1.executeUpdate()

before you initialize it:

mydb1=new Database_CustomTransaction(getApplicationContext());
ci_
  • 8,594
  • 10
  • 39
  • 63
2

You should initialize your mydb1 Object in onCreate(...)

mydb1=new Database_CustomTransaction(getApplicationContext());

before updatedate();

M D
  • 47,665
  • 9
  • 93
  • 114