0

here is the code.

public class DatabaseManager extends android.app.Activity
{
    public SQLiteDatabase mydatabase ;
    private static final Object MODE_PRIVATE = null;
    public static final String TABLE_NAME = "Fests";

    // this function is called first.
    public  void createDatabase()
    {
        try{

    mydatabase = openOrCreateDatabase("fests",SQLiteDatabase.CREATE_IF_NECESSARY,null);
    System.out.println("no probs with open function.");
    mydatabase.execSQL("CREATE TABLE IF NOT EXISTS Fests (FestId VARCHAR,Festname VARCHAR);");
    }catch(Exception e)
    {
        Log.d("creating db","exception caught."+e);
    }
}

I am getting null pointer exception in the first line itself i.e. openOrCreateDatabase function, unable to figure out why. I am new to android programming.Please help me.

saisandeep
  • 63
  • 1
  • 7

2 Answers2

1

I think you are using public class DatabaseManager extends android.app.Activity which is wrong change it to public class DatabaseManager extends SQLiteOpenHelper

CodeWarrior
  • 5,026
  • 6
  • 30
  • 46
0

As mentioned above, change what you class extends.

You also don't need your variable mydatabase. There is already one in the class called database. It will handle the creation of it for you, if you supply the name & version.

E.g.

private static final String DATABASE_NAME = "Name.db";
private static final int DATABASE_VERSION = 2;

/**
 * this gets called automatically on opening the database
 * 
 * @param context
 */
public DatabaseManager(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

// if the database does not exist this method is called to create it
@Override
public void onCreate(SQLiteDatabase database) {
    for (String CREATE_TABLE : CREATE_TABLES)
    {
        database.execSQL(CREATE_TABLE);
    }
}

// when the version changes this method updates the version number and
// creates a log entry
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(SQLiteHelper.class.getName(), "Upgrading database from version "
            + oldVersion + " to " + newVersion
            + ", which will destroy all old data");
    for (String TABLE: TABLES)
    {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE);
    }
    onCreate(db);
}
Gentatsu
  • 696
  • 1
  • 6
  • 26