从SQLite数据库Android中检索值

时间:2017-11-06 12:59:41

标签: android sqlite nullpointerexception

感谢迄今为止的所有帮助。我已成功创建了我的数据库,并使用sqlite浏览器进行了查看。我想分别检索每列中的值。这是我在DBHelper类中的代码:

public TingTingUser getCurrentUser(int id){
    SQLiteDatabase currDB = databaseManager.getWritableDatabase();
    Cursor cursor = currDB.query(true, TABLE_NAME, new String[]{COL_ID, COL_USER_ID, COL_FULL_NAME, COL_GENDER, COL_DOB, COL_MOBILE_NUM, COL_OCCUPATION, COL_ORGANIZATION}, COL_ID + "=?", new String[]{String.valueOf(id)}, null, null, null, null);

    if (cursor != null && cursor.moveToFirst()){
        TingTingUser user = new TingTingUser(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7));
        return user;
    }

    return null;

}

在log-cat中,值正确,但在我的活动class中,它会抛出异常。这是我检索单个值的代码:

我的DatabaseUtils课程:

public class DatabaseUtils extends AppCompatActivity {

private static final String TAG = DatabaseUtils.class.getSimpleName();

protected static DBHelper helper;
public static Context context;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    helper = new DBHelper(this);
    context = this;

}

public static String getUserPhoneNumber(){
    TingTingUser user = helper.getCurrentUser(1);
    String number = user.getMobileNumber();

    Log.d(TAG, "Phone Number from DB is:\t" + number);

    if (number == null){
        return null;
    }
    return number;
}

public static String getUserNameFromDB(){
    TingTingUser user = helper.getCurrentUser(1);
    String name = user.getDisplayName();

    if (name == null){
        return null;
    }
    return name;

}

public static boolean saveGalleryImageToDB(Uri uri){
    helper.open();

    InputStream inputStream = null;
    try {
        inputStream = context.getContentResolver().openInputStream(uri);
        byte[] inputData = AppUtils.ImageUtils.getBytes(inputStream);
        helper.saveGalleryImage(inputData);
        helper.close();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        helper.close();
    }
    return false;

}

public static boolean loadImage(Context context, ImageView imageView){
    try{
        helper.open();
        byte[] bytes = helper.getImage();
        helper.close();
        Bitmap bitmap = AppUtils.ImageUtils.getImage(bytes);
        Uri uri = AppUtils.ImageUtils.getImageUri(context, bitmap);
        imageView.setImageBitmap(AppUtils.ImageUtils.getImage(bytes));
        return true;
    } catch (Exception ex){
        ex.printStackTrace();
    }
    return false;
}

}

检索活动示例代码中的电话号码:

String phone = DatabaseUtils.getUserPhoneNumber();
Log.d(TAG, "Phone Number form DB is:\t" + phone);

TingTingUser是我的用户的模型类,在DatabaseUtils类中演示了基本信息。

  

logcat中的异常消息:

     

引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'com.billionusers.tingting.model.TingTingUser com.billionusers.tingting.db.DBHelper.getCurrentUser(int)'                                                        at com.billionusers.tingting.db.DatabaseUtils.getUserPhoneNumber(DatabaseUtils.java:39)                                                        at com.billionusers.tingting.activities.EditProfileActivity.onCreate(EditProfileActivity.java:89)                                                        在android.app.Activity.performCreate(Activity.java:5990)                                                        在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)                                                        在android.app.ActivityThread.access $ 800(ActivityThread.java:151)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                        在android.os.Looper.loop(Looper.java:135)                                                        在android.app.ActivityThread.main(ActivityThread.java:5254)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在java.lang.reflect.Method.invoke(Method.java:372)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

如何解决这个错误的家伙? 感谢。

这是我桌子的快照: enter image description here

我想分别获取列值。请看看并指导我。再次感谢你们。

2 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为您没有初始化dbHelper。您正在活动onCreate方法初始化,这意味着如果您从其他活动调用该静态方法,您将能够访问该方法由于静态。正如您所建议的那样,您从profileactity调用,这意味着onCreate不存在。并且由于这个dbHelper没有初始化。要克服这个问题,你应该在该方法中初始化dbhelper(getUserPhoneNumber()) HOPE帮助你

答案 1 :(得分:0)

签入数据库对象的初始化。

nullpointer异常是在未正确初始化数据库对象时引起的。