I'm building a simple login app, i have written a simple sqlite for adding user and for checking user for log in. once i add a user i get a null pointer error and my app crashes below is code. the null pointer is found when adding the users using the add method at the register class below
Regsiter class the crash happens here.
private Button register;
private EditText names;
private EditText username;
private EditText password;
private DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
register = (Button)findViewById(R.id.regis);
names = (EditText)findViewById(R.id.name);
username =(EditText)findViewById(R.id.user);
password =(EditText)findViewById(R.id.pass);
register.setOnClickListener(new View.OnClickListener() {
String n = names.getText().toString();
String u = username.getText().toString();
String p = password.getText().toString();
@Override
public void onClick(View v) {
db.addUser(new User(n,u,p));
}
});
}
DatabseClass
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "User.db";
private static final String TABLE_USER = "user";
private static final String COLUMN_USER_NAME = "user_name";
private static final String COLUMN_USER_USERNAME = "user_username";
private static final String COLUMN_USER_PASSWORD = "user_password";
private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "("
+ COLUMN_USER_USERNAME + " varchar(50) primary key not null,"
+ COLUMN_USER_NAME + " TEXT," + COLUMN_USER_PASSWORD + " TEXT" + ")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_USER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//Drop User Table if exist
db.execSQL(DROP_USER_TABLE);
// Create tables again
onCreate(db);
}
public void addUser(User user) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_USER_NAME, user.getName());
values.put(COLUMN_USER_USERNAME, user.getUsername());
values.put(COLUMN_USER_PASSWORD, user.getPassword());
// Inserting Row
db.insertWithOnConflict(TABLE_USER, null, values,SQLiteDatabase.CONFLICT_ABORT);
db.close();
}
public boolean checkUser(String username,String password){
// array of columns to fetch
String[] columns = {
TABLE_USER
};
SQLiteDatabase db = this.getReadableDatabase();
// selection criteria
String selection = COLUMN_USER_USERNAME + " = ?" + " AND " + COLUMN_USER_PASSWORD + " = ?";
// selection arguments
String[] selectionArgs = {username, password};
// query user table with conditions
/**
* Here query function is used to fetch records from user table this function works like we use sql query.
* SQL query equivalent to this query function is
* SELECT user_id FROM user WHERE user_email = 'jack@androidtutorialshub.com' AND user_password = 'qwerty';
*/
Cursor cursor = db.query(TABLE_USER, //Table to query
columns, //columns to return
selection, //columns for the WHERE clause
selectionArgs, //The values for the WHERE clause
null, //group the rows
null, //filter by row groups
null); //The sort order
int cursorCount = cursor.getCount();
cursor.close();
db.close();
if (cursorCount > 0) {
return true;
}
return false;
}