0

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(SQLiteOpenHelper.java:223)
                                                                              at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                              at net.purplebug.mydoctorfinder.MyDBHandler.showDoctors(MyDBHandler.java:133)
                                                                              at net.purplebug.mydoctorfinder.HomeFragment.showDoctorList(HomeFragment.java:131)

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) {
        e.printStackTrace();
    }

    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) + " ");

                row.addView(textView);
            }
            tableLayout.addView(row);
        }

    } 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++;
    }
    index = 0;
    cursor.close();
    db.close();
    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?

Chris Mikkelsen
  • 3,987
  • 9
  • 29
  • 41

4 Answers4

1

If you want to get application context in fragments then you can use getActivity().getApplicationContext()

Note:- Please consider the lifecycle of fragment and make sure the fragment is attached to activity.

Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
0

You just have to do:

`Context context;`
context = getActivity();

and you can use it anywhere in the fragment.

Abhishek Lodha
  • 737
  • 2
  • 7
  • 30
0

you can define a global variable:

private Context context;

and in the onCreate method, initialize it :

context = this.getActivity();

Later use it as following to avoid NullPointerException:

if(context!=null){
//your code
}

And by that you can use the context variable in all your fragment functions/methods.

Adnan
  • 1,440
  • 2
  • 18
  • 38
0

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...
   }
R_K
  • 803
  • 1
  • 7
  • 18