0

I'm using Android Library SQLiteAssetHelper to Read Sqlite database file ,and to populate my RecycleryView from the SQLite database ...

Here's my DataBase Helper :

public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "db.sql";
private static final int DATABASE_VERSION = 1;
private static final String ID="ID";
private static final String NAME="Name";
private static final String IMAGE="Img";
private static final String Location="Location";
private static final String Offices_Table="Offices";



public DBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public ArrayList<Office> GetOffices(){
    SQLiteDatabase sqLiteDatabase=getWritableDatabase();
    String[] columns={DBHelper.ID,DBHelper.NAME,DBHelper.IMAGE,DBHelper.Location};
    Cursor cursor=sqLiteDatabase.query(DBHelper.Offices_Table, columns, null, null, null, null, null);
    ArrayList<Office> offices = new ArrayList<>();

    while(cursor.moveToNext()){
        Office office=new Office();
        office.officeID=cursor.getInt(cursor.getColumnIndex(DBHelper.ID));
        office.officeName=cursor.getString(cursor.getColumnIndex(DBHelper.NAME));

        office.officeLocation=cursor.getString(cursor.getColumnIndex(DBHelper.Location));
        offices.add(office);
    }
    return offices;
}

@Override
public void onCreate(SQLiteDatabase db) {}

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

And this is my POJO class Office.java :

public class Office {

public int officeID;
public String officeName;
public   int officeImg;
public String officeInfo;
public int officeRate;
public String officeLocation;

public Office(String officeName, int officeImg , String officeLocation) {
    this.officeName = officeName;
    this.officeImg = officeImg;
    this.officeLocation=officeLocation;
}

public Office() {
}

public Office(int officeID, String officeName, int officeImg, String officeInfo, int officeRate, String officeLocation) {
    this.officeID = officeID;
    this.officeName = officeName;
    this.officeImg = officeImg;
    this.officeInfo = officeInfo;

    this.officeRate = officeRate;
    this.officeLocation = officeLocation;
}

public void setOfficeID(int officeID) {
    this.officeID = officeID;
}

public void setOfficeName(String officeName) {
    this.officeName = officeName;
}

public void setOfficeImg(int officeImg) {
    this.officeImg = officeImg;
}

public void setOfficeInfo(String officeInfo) {
    this.officeInfo = officeInfo;
}

public void setOfficeRate(int officeRate) {
    this.officeRate = officeRate;
}

public void setOfficeLocation(String officeLocation) {
    this.officeLocation = officeLocation;
}

public int getOfficeID() {
    return officeID;
}

public String getOfficeName() {
    return officeName;
}

public int getOfficeImg() {
    return officeImg;
}

public String getOfficeInfo() {
    return officeInfo;
}

public int getOfficeRate() {
    return officeRate;
}

public String getOfficeLocation() {
    return officeLocation;
}}

Initialize DatabaseHelper file and load data from sqlite database file to Application in OfficesActivity.java file

public class Offices extends AppCompatActivity {

RecyclerView recyclerView ;
OfficeAdapter officeAdapter;
List<Office> officeDataList;
List<Integer> officesImg;
DBHelper dbHelper;
Cursor cursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_offices);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview_paths);
    officeDataList = dbHelper.GetOffices();
    officeAdapter = new OfficeAdapter(this , officeDataList);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setAdapter(officeAdapter);

}}

The Tracetrack Log:

                                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.artline.ministryoftourismpalestine/com.artline.ministryoftourismpalestine.Offices}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList com.artline.ministryoftourismpalestine.DBHelper.GetOffices()' on a null object reference

From the tractrack i know the Office class attributes are null and the OfficeAdapter takes empty list , But how can i solve the problem ? What is the cause?

The problem is solved , thank to VSB ..

But i'm getting error that saying there's not such a table ..

The log error:

                                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.artline.ministryoftourismpalestine/com.artline.ministryoftourismpalestine.Offices}: android.database.sqlite.SQLiteException: no such table: Offices (code 1): , while compiling: SELECT ID, Name, Img, Location FROM Offices
yo aq
  • 5
  • 5
  • Possible duplicate of [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) – ADM Mar 09 '18 at 17:39
  • There's no such table because you never created it. There needs to be code in DbHelper.onCreate(SQLiteDatabase db). – Robin Davies Mar 09 '18 at 21:30
  • @RobinDavies But my database is already created why do i need to use onCreate ? – yo aq Mar 10 '18 at 17:43

1 Answers1

1

You've missed to initialize your DBHelper.

public class Offices extends AppCompatActivity {
...
DBHelper dbHelper = new DBHelper (this);

....
}
VSB
  • 9,825
  • 16
  • 72
  • 145