I have been having trouble with my accessing my database file, I'm not exactly sure where the issue is and i've looked through other answers to this error with no success . I think it's something to do with the main activity class not creating an instance of the IntDatabaseHelper at the right time , but i have been looking at everything for so long that I'm not exactly sure whats wrong . all my code and my Logcat output is below.
MainActivity
public class MainActivity extends AppCompatActivity {
IntDataBaseHelper intDataBaseHelper;
ListView lstJob;
ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intDataBaseHelper = new IntDataBaseHelper(this);
/*create instance of db helper and jobs
Create the database (only if it doesn't exists)
does so by copying from the assets */
LoadJobList();
if (CopyDBFromAssets.createDataBase(this,IntDataBaseHelper.DB_TABLE)) {
// problem area
// Get the data from the database
lstJob = (ListView) findViewById(R.id.lstJob);
ArrayList<String> jobs = intDataBaseHelper.getJobList();
for (String s : jobs) {
Log.d("JobList ", "Found Job " + s);
}
} else {
throw new RuntimeException("No Usable Database exists or was copied from the assets.");
}
}
// loads job to screen
public void LoadJobList() {
ArrayList<String> JobList = intDataBaseHelper.getJobList();
if (mAdapter == null) {
mAdapter = new ArrayAdapter<>(this,R.layout.header,R.id.header);
mAdapter = new ArrayAdapter<>(this,R.layout.row,R.id.BtnComplete,JobList);
mAdapter = new ArrayAdapter<>(this, R.layout.row, R.id.Job_name,JobList);
lstJob.setAdapter(mAdapter);
} else
{
mAdapter.clear();
mAdapter.addAll(JobList);
mAdapter.notifyDataSetChanged();
}
}
}
IntDataBase
public class IntDataBaseHelper extends SQLiteOpenHelper{
private static String DB_PATH ="//data/data/com.example.joelg.clapp/databases";
public static String DB_NAME = "JobList.db";
public static String DB_COLUMN = "jobNM";
public static String DB_TABLE = "job";
private static String DB_JOB_DETAILS = "jobDetails";
private static String DB_ISDONE = "jobIsDone";
private Context jobContext;
private SQLiteDatabase JobListDatabase;
/**
* constructor creater
*/
public IntDataBaseHelper (Context context) {
super (context, DB_NAME,null, 1);
this.jobContext = context;
DB_PATH = jobContext.getDatabasePath(DB_NAME).getPath();
}
public void OpenDataBase() {
// open the database
String JobListPath = DB_PATH;
JobListDatabase = SQLiteDatabase.openDatabase(JobListPath,null,SQLiteDatabase.OPEN_READONLY);
}
// Getting Job Count
public ArrayList<String> getJobList() {
ArrayList<String> JobList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(DB_TABLE,new String[]
{DB_COLUMN},null,null,null,null,null);
while(cursor.moveToNext()){
int index = cursor.getColumnIndex(DB_COLUMN);
JobList.add(cursor.getString(index));
}
cursor.close();
db.close();
return JobList;
}
// Gets the job state if it has been competed or not
public ArrayList<String> getIsDone() {
ArrayList<String> IsDone = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(DB_TABLE,new String[]{DB_ISDONE},null,null,null,null,null);
while(cursor.moveToNext()){
int index = cursor.getColumnIndex(DB_ISDONE);
IsDone.add(cursor.getString(index));
}
cursor.close();
db.close();
return IsDone;
}
@Override
public synchronized void close(){
if(JobListDatabase !=null){
JobListDatabase.close();
super.close();
}
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
LogCat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.joelg.clapp/com.example.joelg.clapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.joelg.clapp.MainActivity.LoadJobList(MainActivity.java:55)
at com.example.joelg.clapp.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
At com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)