0

Not sure why, but I get some weird error. Console says: "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"

DBHandler

public class MyDatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME="CategoryDatabase";

public MyDatabaseHelper(Context context) {

    super(context, DATABASE_NAME, null, 1);

}

@Override

public void onCreate(SQLiteDatabase database) {

    database.execSQL("CREATE TABLE Category (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);");

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS Category");

    onCreate(db);

}

public void addCategory(String name)

{

    ContentValues values=new ContentValues(1);

    values.put("name", name);

    getWritableDatabase().insert("Category", "name", values);

}

public Cursor getCategories()

{

    Cursor cursor = getReadableDatabase().rawQuery("select * from Category", null);

    return cursor;

}

public ArrayList<Category> GetAllValues()
{
    ArrayList<Category> list = new ArrayList<Category>();
    Cursor cursor =  getCategories();
    if (cursor.moveToFirst())
    {
        do
        {
            Category cat = new Category(cursor.getString(0));
            list.add(cat);
        }
        while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed())
    {
        cursor.close();
    }

    return list;
   }
JasonM
  • 3
  • 6
  • Wherever you're instantiating your `MyDatabaseHelper`, it seems the `Context` you're passing in the constructor is null. If you're using `getActivity()`, that's going to be null until `onAttach()` runs. – Mike M. Nov 20 '16 at 13:22
  • @MikeM. Huhh, any ideas how to fix that problem? – JasonM Nov 20 '16 at 15:03
  • Yeah, don't instantiate that helper until after `getActivity()` can return a valid `Context`; e.g., in `onAttach()`, `onCreate()`, etc., as suggested in the duplicate I just linked. – Mike M. Nov 20 '16 at 15:11
  • @MikeM. I moved MyDatabaseHelped inside onCreateView method, but my app still crashes. – JasonM Nov 20 '16 at 16:38
  • What's the stack trace say now? Sure it's not a different error? – Mike M. Nov 20 '16 at 17:11
  • @MikeM. It says something about nullptr related with recyclerview. I went through twice my code, but didn't see anything missing. – JasonM Nov 20 '16 at 17:21
  • If you mean it says something like `Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference`, then it would seem that the `RecyclerView` with ID `recycler_view` is not in the `rec_item` layout. – Mike M. Nov 20 '16 at 17:25
  • @MikeM. Well, then I should change rec_item with rec_list, but then app crashes again. I edited post - maybe you can look into xml files and id's. I guess too much coding for past 48hr and I can't see straight. – JasonM Nov 20 '16 at 17:33
  • "Crashes" is not a good problem description. Read your stack trace. Do a little debugging. Search for the Exceptions you're getting. – Mike M. Nov 20 '16 at 17:48
  • @MikeM. "java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView$State, int, int)' on a null object reference" I can't find a reason why it's acting like that – JasonM Nov 20 '16 at 17:56
  • You never actually set `mLayoutManager` on the `RecyclerView`; you just instantiate it. `rv.setLayoutManager(mLayoutManager);` – Mike M. Nov 20 '16 at 18:00
  • @MikeM. Thanks that worked, but the result is not exactly what I wanted. I just get a recyclerview list without the interface. Weird. – JasonM Nov 20 '16 at 18:06

0 Answers0