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) {
}
}