public class DatabaseHandler extends SQLiteOpenHelper {
//COLUMNS OF THE NEW USER TABLE
public static final String TABLE_NEWUSER = "newUser";
public static final String COLUMN_NEWUSER_ID = "id";
public static final String COLUMN_NEWUSER_NAME = "name";
public static final String COLUMN_NEW_USER_PASSWORD = "password";
public static final String COLUMN_NEW_USER_AGE = "age";
//COLUMNS OF THE BALANCE TABLE
public static final String COLUMN_BALANCE_ID = "id";
public static final String TABLE_BALANCE = "balanceOfUser";
public static final String COLUMN_BALANCE_DOLLARBALANCE = "dollarBalance";
public static final String COLUMN_BALANCE_RUBBALANCE = "rubBalance";
public static final String COLUMN_BALANCE_NEW_USER_ID = "newUserId";
private static final String DATABASE_NAME = "webStore";
private static final int DATABASE_VERSION = 1;
private static final String SQL_CREATE_NEWUSER = "CREATE TABLE " + TABLE_NEWUSER + "("
+ COLUMN_NEWUSER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_NEWUSER_NAME + " TEXT NOT NULL, "
+ COLUMN_NEW_USER_PASSWORD + " TEXT NOT NULL, "
+ COLUMN_NEW_USER_AGE + " INTEGER NOT NULL"
+ ");";
private static final String SQL_CREATE_BALANCE = "CREATE TABLE" + TABLE_BALANCE + "("
+ COLUMN_BALANCE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_BALANCE_DOLLARBALANCE + " INTEGER NOT NULL"
+ COLUMN_BALANCE_RUBBALANCE + " INTEGER NOT NULL"
+ COLUMN_BALANCE_NEW_USER_ID + " INTEGER NOT NULL"
+ ");";
public DatabaseHandler(Context context){
super(context,DATABASE_NAME,null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_BALANCE);
db.execSQL(SQL_CREATE_NEWUSER);
onCreate(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_BALANCE);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NEWUSER);
}
public DatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory,int version){
super(context,DATABASE_NAME,factory,DATABASE_VERSION);
}
}
public void CreateUser(View view) {
etName = (EditText)findViewById(R.id.etName);
etPassword = (EditText)findViewById(R.id.etPassword);
etAge = (EditText)findViewById(R.id.etAge);
String name = String.valueOf(etName.getText());
String password = String.valueOf(etPassword.getText());
int age = Integer.parseInt(String.valueOf(etAge.getText()));
//write to database of user from our edit texts
DatabaseHandler databaseHandler = new DatabaseHandler(this);
Log.d("Insert: ", "Inserting ..");
NewUserDAO dbForUser = new NewUserDAO(this);
dbForUser.createNewUser(name,password,age);
}
I have 2 tables and in table balance i have key of user. When i create new user (createUser method) i have exception:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long android.database.sqlite.SQLiteDatabase.insert(java.lang.String, java.lang.String, android.content.ContentValues)' on a null object reference.
Where is the problem? thanks
Aucun commentaire:
Enregistrer un commentaire