我在使用我的数据库检查用户的用户名和密码时遇到问题。我使用查询来选择特定行,然后根据用户输入的内容检查密码。当我调用
时,我得到的错误是我的登录页面中的java.lang.NullPointerException Users userlogin = db.userlogin(usernameinput);
在查看方法之后,我认为它是第一个导致它失败的游标
cursor.getInt(0);
我想知道的是我是否正确地思考这一点以及我该怎么做才能改变它? 我尝试将if语句更改为
If(cursor.getcount() > 0)
但仍然没有运气。
public class LoginPage extends ActionBarActivity {
Button loginbutton;
EditText usernameuser, passworduser;
DatabaseHandler db;
String usernameinput, passwordinput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
loginbutton = (Button) findViewById(R.id.loginbtn);
usernameuser = (EditText) findViewById(R.id.usernameInsert);
passworduser = (EditText) findViewById(R.id.passwordInsert);
loginbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
usernameinput = usernameuser.getText().toString();
passwordinput = passworduser.getText().toString();
Users userlogin = db.userLogin(usernameinput);
if (usernameinput.equals(userlogin.get_username()) && passwordinput.equals(userlogin.get_password())) {
startActivity(new Intent(getApplicationContext(), Home_Page.class));
}
else {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
});
数据库处理程序用于检查登录的查询:
public Users userLogin(String username) {
SQLiteDatabase db = this.getReadableDatabase();
String[] projection = {KEY_USER_ID, KEY_USERNAME, KEY_PASSWORD};
String selection = KEY_USERNAME + " =?";
String[] selectionargs = {username};
Cursor cursor = db.query(TABLE_USERS, projection, selection, selectionargs, null, null,null );
if (cursor != null)
cursor.moveToFirst();
Users users = new Users(
cursor.getInt(0),
cursor.getString(1),
cursor.getString(2),
cursor.getInt(3),
cursor.getString(4),
cursor.getString(5),
cursor.getDouble(6),
cursor.getDouble(7),
cursor.getDouble(8),
cursor.getDouble(9),
cursor.getDouble(10),
cursor.getDouble(11),
cursor.getDouble(12),
cursor.getDouble(13),
cursor.getDouble(14),
cursor.getDouble(15),
cursor.getDouble(16),
cursor.getDouble(17),
cursor.getDouble(18),
cursor.getDouble(19));
cursor.close();
return users;
}
04-08 13:05:33.194 2565-2565/com.example.john.fitnessapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.john.fitnessapp, PID: 2565
java.lang.NullPointerException
at com.example.john.fitnessapp.LoginPage$1.onClick(LoginPage.java:42)
at android.view.View.performClick(View.java:4569)
at android.view.View$PerformClick.run(View.java:18553)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5137)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
at dalvik.system.NativeStart.main(Native Method)
用户类:
public class Users {
int _id, _age;
String _username, _password, _email, _gender;
double _startWeight, _currentWeight, _weightChange, _height, _BMI, _BMR, _reqCal, _monCal, _tuesCal, _wedCal, _thurCal, _friCal, _satCal, _sunCal;
public Users(){}
public Users(int _id, String _username, String _password, int _age, String _email, String _gender, double _height, double _startWeight,
double _currentWeight, double _weightChange, double _BMI, double _BMR, double _reqCal, double _monCal, double _tuesCal, double _wedCal,
double _thurCal, double _friCal, double _satCal, double _sunCal){
this._id = _id;
this._username = _username;
this._password = _password;
this._age = _age;
this._email = _email;
this._gender = _gender;
this._height = _height;
this._startWeight = _startWeight;
this._currentWeight = _currentWeight;
this._weightChange = _weightChange;
this._BMI = _BMI;
this._BMR = _BMR;
this._reqCal = _reqCal;
this._monCal = _monCal;
this._tuesCal = _tuesCal;
this._wedCal = _wedCal;
this._thurCal = _thurCal;
this._friCal = _friCal;
this._satCal = _satCal;
this._sunCal = _sunCal;
}
public int get_id(){
return this._id;
}
public void set_id(int id){
this._id = id;
}
public String get_username(){
return this._username;
}
public void set_username(String username){
this._username = username;
}
public String get_password(){
return this._password;
}
public void set_password(String password){
this._password = password;
}
public int get_age(){
return this._age;
}
public void set_age(int age){
this._age = age;
}
public String get_email(){
return this._email;
}
public void set_email(String email){
this._email = email;
}
public String get_gender(){
return this._gender;
}
public void set_gender(String gender){
this._gender = gender;
}
public double get_height(){
return this._height;
}
public void set_height(double height){
this._height = height;
}
public double get_startWeight(){
return this._startWeight;
}
public void set_startWeight(double startWeight){
this._startWeight = startWeight;
}
public double get_currentWeight(){
return this._currentWeight;
}
public void set_currentWeight(double currentWeight){
this._currentWeight = currentWeight;
}
public double get_weightChange(){
return this._weightChange;
}
public void set_weightChange(){
this._weightChange = _currentWeight - _startWeight;
}
public double get_BMI(){
return this._BMI;
}
public void set_BMI(double BMI){
this._BMI = BMI;
}
public double get_BMR(){
return this._BMR;
}
public void set_BMR(double BMR){
this._BMR = BMR;
}
public double get_reqCal(){
return this._reqCal;
}
public void set_reqCal(double reqCal){
this._reqCal = reqCal;
}
public double get_monCal(){
return this._monCal;
}
public void set_monCal(double monCal){
this._monCal = monCal;
}
public double get_tuesCal(){
return this._tuesCal;
}
public void set_tuesCal(double tuesCal){
this._tuesCal = tuesCal;
}
public double get_wedCal(){
return this._wedCal;
}
public void set_wedCal(double wedCal){
this._wedCal = wedCal;
}
public double get_thurCal(){
return this._thurCal;
}
public void set_thurCal(double thurCal){
this._thurCal = thurCal;
}
public double get_friCal(){
return this._friCal;
}
public void set_friCal(double friCal){
this._friCal = friCal;
}
public double get_satCal(){
return this._satCal;
}
public void set_satCal(double satCal){
this._satCal = satCal;
}
public double get_sunCal(){
return this._sunCal;
}
public void set_sunCal(double sunCal){
this._sunCal = sunCal;
}
}
数据库处理器:
public class DatabaseHandler extends SQLiteOpenHelper {
public static final String TAG = "DBHelper";
//DATABASE VERSION
private static int DATABASE_VERSION = 1;
//DATABASE NAME
private static final String DATABASE_NAME = "AppDatabase";
//TABLE NAMES
private static final String TABLE_USERS = "Users_Table";
private static final String TABLE_PRODUCTS = "Products_Table";
//COMMON COLUMN NAMES
private static final String KEY_USER_ID = "User_ID";
private static final String KEY_PRODUCT_ID = "Product_ID";
//USER TABLE
private static final String KEY_USERNAME = "Username";
private static final String KEY_PASSWORD = "Password";
private static final String KEY_AGE = "Age";
private static final String KEY_EMAIL = "Email";
private static final String KEY_GENDER = "Gender";
private static final String KEY_HEIGHT = "Height";
private static final String KEY_CURRENT_WEIGHT = "Current_Weight";
private static final String KEY_START_WEIGHT = "Start_Weight";
private static final String KEY_WEIGHT_CHANGE = "Weight_Change";
private static final String KEY_BMI = "BMI";
private static final String KEY_BMR = "BMR";
private static final String KEY_REQ_CAL = "Required_Calories";
private static final String KEY_MON_CAL = "Monday_Calories";
private static final String KEY_TUES_CAL = "Tuesday_Calories";
private static final String KEY_WED_CAL = "Wednesday_Calories";
private static final String KEY_THUR_CAL = "Thursday_Calories";
private static final String KEY_FRI_CAL = "Friday_Calories";
private static final String KEY_SAT_CAL = "Saturday_Calories";
private static final String KEY_SUN_CAL = "Sunday_Calories";
//PRODUCT TABLE
private static final String KEY_ITEMNAME = "Item_name";
private static final String KEY_ITEMCALORIES = "Item_Calories";
//
private static final String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USERS + "( "
+ KEY_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_USERNAME + " TEXT, "
+ KEY_PASSWORD + " TEXT, "
+ KEY_AGE + " INTEGER, "
+ KEY_EMAIL + " TEXT, "
+ KEY_GENDER + " TEXT, "
+ KEY_HEIGHT + " DOUBLE, "
+ KEY_START_WEIGHT + " DOUBLE, "
+ KEY_CURRENT_WEIGHT + " DOUBLE, "
+ KEY_WEIGHT_CHANGE + " DOUBLE, "
+ KEY_BMI + " DOUBLE, "
+ KEY_BMR + " DOUBLE, "
+ KEY_REQ_CAL + " DOUBLE, "
+ KEY_MON_CAL + " DOUBLE, "
+ KEY_TUES_CAL + " DOUBLE, "
+ KEY_WED_CAL + " DOUBLE, "
+ KEY_THUR_CAL + " DOUBLE, "
+ KEY_FRI_CAL + " DOUBLE, "
+ KEY_SAT_CAL + " DOUBLE, "
+ KEY_SUN_CAL + " DOUBLE ); ";
private static final String CREATE_PRODUCT_TABLE = "CREATE TABLE " + TABLE_PRODUCTS + "( " + KEY_PRODUCT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_ITEMNAME + " TEXT, "
+ KEY_ITEMCALORIES + " DOUBLE );";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_USER_TABLE);
db.execSQL(CREATE_PRODUCT_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG,
"Upgrading the database from version " + oldVersion + " to " + newVersion);
DATABASE_VERSION = 2;
db.execSQL("DROP TABLE IF IT EXISTS " + TABLE_USERS);
db.execSQL("DROP TABLE IF IT EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
答案 0 :(得分:1)
修改:确保初始化扩展SQLiteOpenHelper
的
请务必致电:
db = new DatabaseHandler(this);
如果您没有初始化db
,那么当您致电db.userLogin(usernameinput);
时它将为空,这可能是您NullPointerException
获得public class LoginPage extends ActionBarActivity {
Button loginbutton;
EditText usernameuser, passworduser;
DatabaseHandler db;
String usernameinput, passwordinput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
db = new DatabaseHandler(this); //initialize the DatabaseHandler
//........
的原因
你可以在`onCreate()'中调用它。像这样:
cursor.moveToFirst()
此外,您似乎只查询数据库的三列,并尝试访问游标中的二十列。
此外,您应该检查public Users userLogin(String username) {
Users users = null;
SQLiteDatabase db = this.getReadableDatabase();
//String[] projection = {KEY_USER_ID, KEY_USERNAME, KEY_PASSWORD};
String selection = KEY_USERNAME + " =?";
String[] selectionargs = {username};
Cursor cursor = db.query(TABLE_USERS, null, selection, selectionargs, null, null,null );
if (cursor.moveToFirst()){
users = new Users(
cursor.getInt(0),
cursor.getString(1),
cursor.getString(2),
cursor.getInt(3),
cursor.getString(4),
cursor.getString(5),
cursor.getDouble(6),
cursor.getDouble(7),
cursor.getDouble(8),
cursor.getDouble(9),
cursor.getDouble(10),
cursor.getDouble(11),
cursor.getDouble(12),
cursor.getDouble(13),
cursor.getDouble(14),
cursor.getDouble(15),
cursor.getDouble(16),
cursor.getDouble(17),
cursor.getDouble(18),
cursor.getDouble(19));
}
cursor.close();
return users;
}
的返回值。
通常,游标永远不会为空。
这可能会使其短期工作,但您可能需要考虑重新分解代码,这样您就不必查询所有行以使其正常工作:
Users
理想情况下,您可以为public Users userLogin(String username) {
Users users = null;
SQLiteDatabase db = this.getReadableDatabase();
String[] projection = {KEY_USER_ID, KEY_USERNAME, KEY_PASSWORD};
String selection = KEY_USERNAME + " =?";
String[] selectionargs = {username};
Cursor cursor = db.query(TABLE_USERS, projection, selection, selectionargs, null, null,null );
if (cursor.moveToFirst()){
users = new Users(
cursor.getInt(0),
cursor.getString(1),
cursor.getString(2))
}
cursor.close();
return users;
}
类创建另一个构造函数,在这种情况下可以只使用您需要的三个参数。
您修改的代码将是这样的:
null
此外,最好从userLogin()
函数中检查loginbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
usernameinput = usernameuser.getText().toString();
passwordinput = passworduser.getText().toString();
Users userlogin = db.userLogin(usernameinput);
if (userlogin != null && usernameinput.equals(userlogin.get_username()) && passwordinput.equals(userlogin.get_password())) {
startActivity(new Intent(getApplicationContext(), Home_Page.class));
}
else {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
});
返回值:
{{1}}