thanks for all the help so far. I have successfully created my database and viewed it with sqlite
browser. I want to retrieve the values in each column separately. This is my code in DBHelper
class:
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;
}
In log-cat, the values are coming correctly but in my activity class
, it throws an exception. Here is my code for retrieving the individual values:
My DatabaseUtils
class:
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;
}
}
Retrieving the phone number in activity sample code:
String phone = DatabaseUtils.getUserPhoneNumber();
Log.d(TAG, "Phone Number form DB is:\t" + phone);
TingTingUser
is my user's model class with basic info demoed in DatabaseUtils
class.
Exception message in logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.billionusers.tingting.model.TingTingUser com.billionusers.tingting.db.DBHelper.getCurrentUser(int)' on a null object reference at com.billionusers.tingting.db.DatabaseUtils.getUserPhoneNumber(DatabaseUtils.java:39) at com.billionusers.tingting.activities.EditProfileActivity.onCreate(EditProfileActivity.java:89) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
How to solve this error guys? Thanks.
Here is a snapshot of my table:
I want to get the column values separately. Please have a look and guide me. Thanks again guys.