0

I just recently started working on a project but my program repeatedly crashes. I think it is due to getWritableDatabase. I was following a tutorial and i did everything they told me to, i even checked over my code multiple times. If u are interested in the link to the tutorial, it is https://www.youtube.com/watch?v=38DOncHIazs

public class AddExpense extends Activity {

EditText inputDate , inputDescription, inputCategory, inputAmount;
Context context;
UserDbHelper userDbHelper;
SQLiteDatabase sqLiteDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_expense);
    inputDate = (EditText) findViewById(R.id.inputDate);
    inputDescription = (EditText) findViewById(R.id.inputDescription);
    inputCategory = (EditText) findViewById(R.id.inputCategory);
    inputAmount = (EditText) findViewById(R.id.inputAmount);

}

public void AddExpense(View view){
    String date = inputDate.getText().toString();
    String description = inputDescription.getText().toString();
    String category = inputCategory.getText().toString();
    String amount = inputAmount.getText().toString();
    userDbHelper = new UserDbHelper(context);
    sqLiteDatabase = userDbHelper.getWritableDatabase();
    userDbHelper.addInformations(date, description, category, amount, sqLiteDatabase);
    Toast.makeText(getBaseContext(),"Data Saved", Toast.LENGTH_LONG).show();
    userDbHelper.close();
}

}

And this is my database helper class

public class UserDbHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "EXPENSE.DB";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_QUERY =
        "CREATE TABLE " + ExpenseDatabase.NewExpenseItem.TABLE_NAME + " ("+ ExpenseDatabase.NewExpenseItem.DATE+" TEXT,"+ ExpenseDatabase.NewExpenseItem.DESCRIPTION+ " TEXT,"+
        ExpenseDatabase.NewExpenseItem.CATEGORY+" TEXT," + ExpenseDatabase.NewExpenseItem.AMOUNT+ " TEXT);";

public UserDbHelper(Context context){


    super(context,DATABASE_NAME,null,DATABASE_VERSION);
    Log.e("DATABASE OPERATIONS", "Database created / opened");
}
@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(CREATE_QUERY);
    Log.e("DATABASE OPERATIONS", "Table created");

}

public void addInformations(String date, String description, String category, String amount,SQLiteDatabase db ){
    ContentValues contentValues = new ContentValues();
    contentValues.put(ExpenseDatabase.NewExpenseItem.DATE,date);
    contentValues.put(ExpenseDatabase.NewExpenseItem.DESCRIPTION, description);
    contentValues.put(ExpenseDatabase.NewExpenseItem.CATEGORY, category);
    contentValues.put(ExpenseDatabase.NewExpenseItem.AMOUNT, amount);
    db.insert(ExpenseDatabase.NewExpenseItem.TABLE_NAME, null, contentValues);
    Log.e("DATABASE OPERATIONS", "One row inserted in DB");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}

2 Answers2

0

In my experience it is better if you just make a method in your UserDbHelper class and pass the relevant values to that method. Try to do everything related to database in that class itself. I think it will work.

EDIT:

    public void addInformation(your values){    
        ContentValues values = new ContentValues();
        values.put();            
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE,null,values);
        db.close();
    }
Jai Saxena
  • 235
  • 2
  • 14
  • TBH it is my first time working with databases, so i dont exactly know what the method getWritableDatabase produces. If u could help me with code or supply me with a link, it would be helpful. – Randomshot6 Jul 09 '15 at 21:34
  • check the updated answer. i dont think u should pass the db object. create it locally in method addInformation() @Randomshot6 – Jai Saxena Jul 09 '15 at 21:44
0

Try using this userDbHelper = new UserDbHelper(this); some you're context is not initialized. It might be the issue

Mightian
  • 6,853
  • 3
  • 37
  • 50