我是安卓新手。我想将数据库中的用户名与活动中输入的值进行比较,并获得 NullPointerException (NPE)。
这是我的 DBHelper 活动
public class DBHelper extends SQLiteOpenHelper
{
private SQLiteDatabase db;
public static final String KEY_ROWID = "_id";
public static final String KEY_ROWID1="_id";
public static final String KEY_FNAME = "firstname";
public static final String KEY_LNAME = "lastname";
public static final String KEY_GENDER = "gender";
public static final String KEY_USER = "username";
public static final String KEY_EMAIL = "email";
public static final String KEY_CNAME="Cname";
public static final String KEY_CPOST="Cpost";
public static final String KEY_CCRITERIA="Ccriteria";
public static final String KEY_CUSER="Cusername";
public static final String KEY_CEMAIL="Cemail";
public static final String KEY_CCONTACT="Ccontact";
public static final String KEY_CCUTOFF="Ccutoff";
public static final String KEY_STATUS="status";
public static final String KEY_USERNAME="username";
DBHelper DB = null;
private static final String DATABASE_NAME = "Sam2.db";
private static final int DATABASE_VERSION = 3;
public static final String DATABASE_TABLE_NAME = "JobSeeker";
public static final String Company_Table = "Comp";
public static final String Login_Table = "Login";
private static final String DATABASE_TABLE_CREATE =
"CREATE TABLE " + DATABASE_TABLE_NAME + "(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
"firstname TEXT NOT NULL, lastname TEXT NOT NULL, gender TEXT NOT NULL, username TEXT NOT NULL UNIQUE, password TEXT NOT NULL, email TEXT NOT NULL);";
private static final String DATABASE_TABLE_CREATE2=
" create table " + Company_Table +
" (_id integer primary key autoincrement," +
" Cname TEXT NOT NULL, Cpost TEXT NOT NULL,Ccriteria TEXT NOT NULL,Cusername TEXT NOT NULL, Cpassword TEXT NOT NULL, Cemail TEXT NOT NULL,Ccontact TEXT NOT NULL,Ccutoff TEXT NOT NULL);" ;
private static final String DATABASE_TABLE_CREATE3=
"CREATE TABLE " + Login_Table + "(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
"username TEXT NOT NULL UNIQUE, password TEXT NOT NULL, status TEXT NOT NULL);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
System.out.println("In constructor");
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
db.execSQL(DATABASE_TABLE_CREATE);
db.execSQL(DATABASE_TABLE_CREATE2);
db.execSQL(DATABASE_TABLE_CREATE3);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public Cursor rawQuery(String string, String[] strings) {
// TODO Auto-generated method stub
return null;
}
public void open() {
getWritableDatabase();
}
public Cursor getDetails(String text) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE_NAME,
new String[]{KEY_ROWID, KEY_FNAME, KEY_LNAME, KEY_GENDER, KEY_USER, KEY_EMAIL},
KEY_USER + "=" + text,
null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getCompDetails(String text) throws SQLException
{
Cursor mCursor =
db.query(true, Company_Table,
new String[]{KEY_ROWID1, KEY_CNAME, KEY_CPOST, KEY_CCRITERIA, KEY_CUSER, KEY_EMAIL,KEY_CCONTACT,KEY_CCUTOFF},
KEY_CUSER + "=" + text,
null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getLoginDetails(String text) throws SQLException
{
Cursor mCursor =
db.query(true, Login_Table,
new String[]{KEY_ROWID1, KEY_USERNAME, KEY_STATUS},
KEY_USERNAME + "=" + text,
null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
public Boolean checkusername(String name) throws SQLException {
Cursor mCursor = db.query(
Login_Table,
new String[] { KEY_ROWID, KEY_USERNAME },
KEY_USERNAME + "=?",
new String[] {name},
null,
null,
null);
if (mCursor!=null) {
mCursor.moveToFirst();
return true;
}
return false;
}
}
这是执行比较的活动。
DB.open();
if (DB.checkusername(uname)) {
invalid = true;
Toast.makeText(getApplicationContext(),
"username alredy in use enter another",
Toast.LENGTH_SHORT).show();
}
它在这一行显示错误
Cursor mCursor = db.query(
Login_Table,
new String[] { KEY_ROWID, KEY_USERNAME },
KEY_USERNAME + "=" + name,
null,
null,
null,
null);
这是logcat
07-07 23:52:56.813: W/dalvikvm(536): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-07 23:52:56.823: E/AndroidRuntime(536): FATAL EXCEPTION: main
07-07 23:52:56.823: E/AndroidRuntime(536): java.lang.NullPointerException
07-07 23:52:56.823: E/AndroidRuntime(536): at com.google.data.DBHelper.checkusername(DBHelper.java:164)
07-07 23:52:56.823: E/AndroidRuntime(536): at com.google.data.Registration.onClick(Registration.java:211)
07-07 23:52:56.823: E/AndroidRuntime(536): at android.view.View.performClick(View.java:2408)
07-07 23:52:56.823: E/AndroidRuntime(536): at android.view.View$PerformClick.run(View.java:8816)
07-07 23:52:56.823: E/AndroidRuntime(536): at android.os.Handler.handleCallback(Handler.java:587)
07-07 23:52:56.823: E/AndroidRuntime(536): at android.os.Handler.dispatchMessage(Handler.java:92)
07-07 23:52:56.823: E/AndroidRuntime(536): at android.os.Looper.loop(Looper.java:123)
07-07 23:52:56.823: E/AndroidRuntime(536): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-07 23:52:56.823: E/AndroidRuntime(536): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 23:52:56.823: E/AndroidRuntime(536): at java.lang.reflect.Method.invoke(Method.java:521)
07-07 23:52:56.823: E/AndroidRuntime(536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-07 23:52:56.823: E/AndroidRuntime(536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-07 23:52:56.823: E/AndroidRuntime(536): at dalvik.system.NativeStart.main(Native Method)