2015-04-07 33 views
0

我在使用我的數據庫檢查用戶的用戶名和密碼時遇到問題。我使用查詢來選擇特定行,然後根據用戶輸入的內容檢查密碼。我得到的錯誤是在我的登錄頁面顯示java.lang.NullPointerException當我打電話用戶使用SQLite數據庫登錄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

仍然得到同樣的錯誤。只是把用戶類放在那裏 –

+0

張貼數據庫那裏我遺漏了CRUD操作 –

+0

它是雅我發現,當我去插入代碼行,你建議.getApplicationContext部分給出了一個錯誤 –

回答

0

編輯:確保初始化您的課程,延伸SQLiteOpenHelper

確保您撥打:

db = new DatabaseHandler(this); 

如果不初始化db當你調用db.userLogin(usernameinput);那麼這將是空的,這可能是你得到了NullPointerException的原因。

你可以稱它在'的onCreate()」這樣的:

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 
      //........ 

此外,它看起來像你只查詢數據庫的三列,並試圖進入二十列光標。

另外,您應該檢查返回值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; 

} 

而且,這將是很好,從您的userLogin()功能檢查null返回值:

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(); 
       } 
      } 

     }); 
+0

感謝您的幫助我創建了一個帶3個參數的新構造函數,並且發生了同樣的錯誤我也有if語句現在詢問遊標!= null是否在登錄代碼 –

+0

中添加了原始文章中的logcat –