0

I know this is a bit common but yet I can't still find the solution, and this is the first app I've been creating. I have this error below which the SQLITE recognizes as a null object reference. I've been confused, now let's say I have two(2) which are first_activity, second_activity when I go directly to the second_activity the error will be shown below but when I open the first_activity first and then back to the dashboard and open the second_activity the data from SQLite show. Is there anything that can I do? it very helpful for me.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor

InventoryList.java

  InventoryListAdapter adapter = null;
  

  //below is code inside Oncreate

  

        //the error refers below which is the cursor

        Cursor cursor = ScannedDetails.sqLiteHelper.getData("SELECT id,cash_card,hh_number,series_number,id_image FROM CgList limit 500");

        list.clear();
        while (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String cashCardNumber = cursor.getString(1);
            String hhNumber = cursor.getString(2);
            String seriesNumber = cursor.getString(3);
            byte[] CashCardImage = cursor.getBlob(4);
            list.add(new Inventory(cashCardNumber, hhNumber,seriesNumber, CashCardImage, id));
        }
        adapter.notifyDataSetChanged();

SQLITEHelper

    public class SQLiteHelper extends SQLiteOpenHelper {

    public SQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    
    public void queryData(String sql){
        SQLiteDatabase database = getWritableDatabase();
        database.execSQL(sql);
    }

    public void insertData(String cash_card, String hh_number,String series_number ,byte[] cc_image,byte[] id_image){
        SQLiteDatabase database = getWritableDatabase();
        String sql = "INSERT INTO CgList VALUES (NULL,?, ?, ?, ?, ?)";

        SQLiteStatement statement = database.compileStatement(sql);
        statement.clearBindings();

        statement.bindString(1, cash_card);
        statement.bindString(2, hh_number);
        statement.bindString(3, series_number);
        statement.bindBlob(4, cc_image);
        statement.bindBlob(5, id_image);
        statement.executeInsert();
    }
    public Cursor getData(String sql){
        SQLiteDatabase database = getReadableDatabase();
        return database.rawQuery(sql, null);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
Henry
  • 165
  • 13
  • 2
    Your error message seems to have been cut off. However, `ScannedDetails.sqLiteHelper` does not look good, considering that you are not filling in a value for that `sqLiteHelper` field, but instead a different `sqLiteHelper` field. – CommonsWare Sep 06 '21 at 11:30
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Selvin Sep 06 '21 at 11:31
  • Where do you create your table? I'd typically expect to see that in `onCreate`. There's quite a lot wrong with this code before we get to the NPE. – PPartisan Sep 06 '21 at 11:49
  • @PPartisan Thanks for your response, Actually I posted incomplete because I just want to show the idea I just create table onCreate in my MainActivity just like this `sqLiteHelper = new SQLiteHelper(this, "CgTracking.sqlite", null, 1); sqLiteHelper.queryData("CREATE TABLE IF NOT EXISTS CgList(Id INTEGER` – Henry Sep 06 '21 at 11:53
  • @CommonsWare Thanks for your response I get your point, yes you're right but I already tried using the public variable `sqLiteHelper` that comes from the `ScannedDetails` and then I keep searching and found out to declare again the `database` that's why I put new `sqLiteHelper` but now I removed, the output sames goes before – Henry Sep 06 '21 at 11:57
  • Its fine now thanks everyone! I should put MainActivity : `Cursor cursor = MainActivity.sqLiteHelper.getData ` instead of ScannedDetails – Henry Sep 06 '21 at 12:07

1 Answers1

0

Its fine now thanks everyone! I should put MainActivity in : Cursor cursor = MainActivity.sqLiteHelper.getData (Select ......)

instead of Cursor cursor = ScannedDetails.sqLiteHelper.getData (Select ......)

Henry
  • 165
  • 13