I keep receiving the following error when running my app and attempting to add a new User to my system.
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
at com.dissertation.anna.rpiinventory.UsersDAO.createUser(UsersDAO.java:60)
at com.dissertation.anna.rpiinventory.Register.onClick(Register.java:68)
The UsersDAO class:
public class UsersDAO {
//DB fields
private SQLiteDatabase aDatabase;
private DatabaseHelper aDbHelper;
Context aContext;
private String[] uAllColumns = {DatabaseHelper.COLUMN_USER_ID, DatabaseHelper.COLUMN_UNI_ID,
DatabaseHelper.COLUMN_USER_NAME, DatabaseHelper.COLUMN_USER_EMAIL, DatabaseHelper.COLUMN_USER_COURSE,
DatabaseHelper.COLUMN_USER_TYPE, DatabaseHelper.COLUMN_ADMIN_PASSWORD};
public UsersDAO(Context context){
this.aContext = context;
aDbHelper = new DatabaseHelper(context);
//open DB
try{
open();
} catch (SQLException e){
Log.e(TAG, "SQLException on opening Database " + e.getMessage());
e.printStackTrace();
}
}
public void open() throws SQLException{
aDatabase = aDbHelper.getWritableDatabase();
}
public void close(){
aDbHelper.close();
}
public Users createUser(String uniID, String name, String email, String course, String type, String password) {
ContentValues values = new ContentValues();
values.put(DatabaseHelper.COLUMN_UNI_ID, uniID);
values.put(DatabaseHelper.COLUMN_USER_NAME, name);
values.put(DatabaseHelper.COLUMN_USER_EMAIL, email);
values.put(DatabaseHelper.COLUMN_USER_COURSE, course);
values.put(DatabaseHelper.COLUMN_USER_TYPE, type);
values.put(DatabaseHelper.COLUMN_ADMIN_PASSWORD, password);
long insertID = aDatabase.insert(DatabaseHelper.TABLE_USERS, null, values);
Cursor cursor = aDatabase.query(DatabaseHelper.TABLE_USERS, uAllColumns, DatabaseHelper.COLUMN_USER_ID
+ " = " + insertID, null, null, null, null);
cursor.moveToFirst();
Users newUser = cursorToUser(cursor);
cursor.close();
return newUser;
}
The onClick method in the Register class
public void onClick(View v){
switch (v.getId()){
case R.id.register_OK_button:
Editable u_adminName= adminName.getText();
Editable u_adminEmail = adminEmail.getText();
Editable u_adminNumber = adminNumber.getText();
Editable u_adminPassword = adminPassword.getText();
String u_userType = "Admin";
String u_course = "N/A";
String idNumber = u_adminNumber.toString();
String name = u_adminName.toString();
String email = u_adminEmail.toString();
String password = u_adminPassword.toString();
if (!TextUtils.isEmpty(u_adminName) && !TextUtils.isEmpty(u_adminEmail)
&& !TextUtils.isEmpty(u_adminNumber) && !TextUtils.isEmpty(u_adminPassword)
&& !TextUtils.isEmpty(u_userType) && !TextUtils.isEmpty(u_course)){
//Add Admin to Database
Users createdAdmin = u_adminDAO.createUser(idNumber, name,
email, u_course,u_userType, password);
Log.d(TAG, "Created admin : " + createdAdmin.getFullName());
Intent intent = new Intent();
intent.putExtra(ListAdminsActivity.EXTRA_ADDED_ADMIN, createdAdmin);
setResult(RESULT_OK, intent);
Toast.makeText(this, R.string.admin_added_successfully, Toast.LENGTH_LONG).show();
finish();
}//if
else{
Toast.makeText(this, "One or more fields are empty.", Toast.LENGTH_LONG).show();
}//else
break;
default:
break;
}//switch
}//onClick
Database Handler:
private static final String DATABASE_NAME = "rpi_inventory_new.db";
private static final int DATABASE_VERSION = 1;
//SQL STATEMENT OF USERS TABLE CREATION
private static final String SQL_CREATE_TABLE_USERS = "CREATE TABLE " + TABLE_USERS + "("
+ COLUMN_USER_ID + " INTEGER AUTOINCREMENT, "
+ COLUMN_UNI_ID + " TEXT PRIMARY KEY, "
+ COLUMN_USER_NAME + " TEXT NOT NULL, "
+ COLUMN_USER_EMAIL + " TEXT NOT NULL, "
+ COLUMN_USER_COURSE + " TEXT NULL, "
+ COLUMN_USER_TYPE + " TEXT NOT NULL, "
+ COLUMN_ADMIN_PASSWORD + " PASSWORD NULL"
+ ");";
public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database){
database.execSQL(SQL_CREATE_TABLE_USERS);
database.execSQL(SQL_CREATE_TABLE_BORROWING);
database.execSQL(SQL_CREATE_TABLE_KITS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
Log.w(TAG,
"Upgrading the database from version " + oldVersion + " to " + newVersion);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_BORROWING);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_KITS);
onCreate(db);
}
public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
}
I have checked various other answers quite thoroughly and none of the answers have worked on my database. I have no idea why it is occurring or what I am doing wrong, I have created another similar database and it works just fine.
The two lines causing the errors are:
long insertID = aDatabase.insert(DatabaseHelper.TABLE_USERS, null, values);
And:
Users createdAdmin = u_adminDAO.createUser(idNumber, name,
email, u_course,u_userType, password);
Aucun commentaire:
Enregistrer un commentaire