Chris Mikkelsen Chris Mikkelsen - 1 year ago 248
Android Question

How to get the Context of an Application in a Fragment

First I have this error message

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(
at net.purplebug.mydoctorfinder.MyDBHandler.showDoctors(
at net.purplebug.mydoctorfinder.HomeFragment.showDoctorList(

So I thought it could be the way I wrote the Context as parameter for the dbhandler instance.

This is the code which triggers the error. It's a method in a class that extends Fragment.

public void showDoctorList() {
String result = "Match Found";

MyDBHandler dbHandler = new MyDBHandler(getContext(), null, null, 1);

ArrayList<ArrayList<String>> docArray = new ArrayList<ArrayList<String>>();

DoctorDataModel doctor = null;
try {
doctor = dbHandler.showDoctors();
} catch (SQLException e) {

if (doctor != null) {

TableRow row = new TableRow(getActivity());
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

docArray = doctor.getDocArray();

for(int i = 0; i < docArray.size(); i++) {
for(int j = 0; j < docArray.get(i).size(); j++) {
TextView textView = new TextView(getActivity());
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView.setPadding(5, 5, 5, 5);
textView.setText(docArray.get(i).get(j) + " ");


} else {
result = "No Match Found";

Log.d("QUERY RESULT", result);


And finally the code in the DBHandler in relation to the error.

public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
this.myContext = context;

public DoctorDataModel showDoctors() throws SQLException {

int index = 0;
String docLastName, docFirstName, docId;

String query = "Select doctor_id, lastName, firstName FROM " + TABLE_DOCTOR;

SQLiteDatabase db = this.getWritableDatabase();

Cursor cursor = db.rawQuery(query, null);

DoctorDataModel doctor = new DoctorDataModel();

while (cursor.moveToNext()) {
docId = cursor.getString(0);
docLastName = cursor.getString(1);
docFirstName = cursor.getString(2);
doctor.setdocArrayList(index, docId, docLastName, docFirstName);
index = 0;
return doctor;

I really think it's with the way I get the Context in this code.

MyDBHandler dbHandler = new MyDBHandler(getContext(), null, null, 1);

Or it could be something else which is the problem. So what do you guys think?

Answer Source

In fragment you can get context by :: getActivity()

Before use it just check, Is fragment attached to activity or not ?

Context context = getActivity();

 if(context != null){
     //write your code here...