Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

sql database problem

Hi, I am learning to build a very basic sql database app and only include the add data function. I hard coded everything because I just wanted to see if the database can be up and running. There is a error whenever I press my button that calls insert data function. Cause: invocation target exception. My code is below, the main activity and databasehandler. Thanks

Main activity: import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View;

public class MainActivity extends AppCompatActivity { DataBaseHandler dba;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
public void button(View view){

  dba.insert("apple",10,4,5,6);



}

} Databasehandler:

public class DataBaseHandler extends SQLiteOpenHelper { public static final String DATABASE_NAME = "MACRO.DB";

public static final String TABLE_NAME = "MACRO.TB";



public DataBaseHandler(Context context) {
    super(context, DATABASE_NAME, null, 1);
    SQLiteDatabase db = getWritableDatabase();
}



@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE MACRO_TABLE (_ID INTEGER PRIMARY KEY AUTOINCREMENT, DATE LONG, FOODNAME TEXT, CALORIES INTEGER, PROTEIN INTEGER, CARBOHYDRATE INTEGER, FAT INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
    onCreate(db);
}

public void insert(String food, Integer cal, Integer pro, Integer carb, Integer fat){

    SQLiteDatabase dba = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("FOODNAME",food);
    values.put("CALORIES", cal);
    values.put("DATE", System.currentTimeMillis());
    values.put("PROTEIN", pro);
    values.put("CARBOHYDRATE","carb");
    values.put("FAT", "fat");

    dba.insert("MACRO_TABLE", null, values);

    Log.v("Added Food item", "Yesss!!");

    dba.close();




}

}

1 Answer

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Chi,

Any chance of posting your logCat. I can see that you aren't creating a new DatabaseHandler object you are trying to call dba.insert() on a null object you need to add dba = new DatabaseHandler(); you also never close the connection inside the Constructor. I understand when this is run the first time you need to run getReadableDatabase() to get the database to install on the device but I would make a separate method for this and definitely close the connection once the database is made.

You also add strings which are hardcoded carb and food when I think you intend to use variables now this wont cause SQLlite to crash (which is annoying at times even on an Integer field) but you should probably avoid it.

And lastly the table is the variable MACRO_TABLE not the string "MACRO_TABLE" which will throw an error and I suspect is the cause.

Hope this helps Daniel