1

In my MainActivity.java I get this error "Cannot instantiate the type userSQLOpenHelper". at new userSQLOpenHelper(this). Can someone tell me what I am missing or doing wrong?

MainActivity.java

userSQLOpenHelper db = new userSQLOpenHelper(this);             
// add user
db.addUser(new user(name1,born1,sex,passwd,email1));   

user.java

public final class user {

    private int id;
    private String name;
    private String born;
    private String sex;
    private int password;
    private String email;

        // This is recommended to prevent someone from accidentally instantiating the user class,
    public user() {}

    /* Inner class that defines the table contents */
    public user(String name, String born, String sex, int password, String email)      {
        // super();
        this.name = name;
        this.born = born;
        this.sex = sex;
        this.password = password;
        this.email = email;
    }

    @Override
    public String toString() {
        return "user [id=" + id + ", name=" + name + ", born=" + born + ", sex=" + sex + ", password=" + password
            + ", email=" + email + "]";
    }

    public int getId(){
        return this.id;
    }

    public void setId(int id){
        this.id = id;
    }

    public String getName(){
        return this.name;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getBorn(){
        return this.born;
    }

    public void setBorn(String born){
        this.born = born;
    }

      public String getSex(){
        return this.sex;
    }

    public void setSex(String sex){
        this.sex = sex;
    }

      public int getPassword(){
        return this.password;
    }

    public void setPassword(int password){
        this.password = password;
    }

    public String getEmail(){
        return this.email;
    }

    public void setEmail(String email){
        this.email = email;
    }
}

userSQLOpenHelper.java

public abstract class userSQLOpenHelper extends SQLiteOpenHelper {

    // Table name
    private static final String TABLE_USERS = "users";

    // Table Columns names
    private static final String USER_ID = "id";
    private static final String USER_NAME = "name";
    private static final String USER_BORN = "born";
    private static final String USER_SEX = "sex";
    private static final String USER_PASSWORD = "password";
    private static final String USER_EMAIL = "email";

    //private static final String[] COLUMNS = {USER_ID,USER_NAME,USER_BORN,USER_SEX,USER_PASSWORD,USER_EMAIL};

    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "userDB";   

  public userSQLOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);  
  }


    @Override
  public void onCreate(SQLiteDatabase db) {
    // SQL statement to create user table
    String CREATE_USER_TABLE = "CREATE TABLE users ( " +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
            "name TEXT NOT NULL, "+ 
            "born TEXT NOT NULL, " +
            "sex TEXT NOT NULL, " +
            "password INTERGER NOT NULL, " +
            "email TEXT NOT NULL )";

    // create books table
    db.execSQL(CREATE_USER_TABLE);

  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older books table if existed
    db.execSQL("DROP TABLE IF EXISTS books");

    // create fresh books table
    this.onCreate(db);
  }


  public void addUser(user user){
    SQLiteDatabase db;
     //get reference to writable DB
     db = this.getWritableDatabase();

    //create ContentValues to add key "column"/value
    ContentValues values = new ContentValues();
    values.put(USER_NAME, user.getName()); 
    values.put(USER_BORN, user.getBorn()); 
    values.put(USER_SEX, user.getSex()); 
    values.put(USER_PASSWORD, user.getPassword()); 
    values.put(USER_EMAIL, user.getEmail()); 

      // insert
    db.insert(TABLE_USERS, null, values); 

    // close
    db.close(); 
    }

}
nem035
  • 34,790
  • 6
  • 87
  • 99
JulieB
  • 25
  • 2
  • 1
    see [Interview: Can we instantiate abstract class?](http://stackoverflow.com/questions/13670991/interview-can-we-instantiate-abstract-class) – ρяσѕρєя K Sep 23 '14 at 16:52

2 Answers2

0

You've declared your class abstract.

Remove that keyword from your userSQLOpenHelper class declaration.

Michael Krause
  • 4,689
  • 1
  • 21
  • 25
0

You cannot instantiate abstract classes...

public abstract class userSQLOpenHelper

Since you do not have any abstract methods inside your class, you can just remove the keyword abstract from your class declaration and that error should disappear...

nem035
  • 34,790
  • 6
  • 87
  • 99