java


Error when accessing a SQlite database in android studio


Following my last post i was using SQLiteOpenHelper to access a pre-populated sqlite database and someone suggested i use SQLAssetHelper so i found a guide followed it and tried to implement it but i am still getting errors.
like: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
I know what a null pointer exception is in most cases but dont understand it in this context as i am new to android development. The errors on line 26 of my DBHleper class below. - String dbPath = myContext.getDatabasePath(DATABASE_NAME).getPath();
public class DBHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "Database.db";
private static final int DATABASE_VERSION = 1;
private Context myContext;
private SQLiteDatabase mDatabase;
public DBHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
public void openDatabase(){
String dbPath = myContext.getDatabasePath(DATABASE_NAME).getPath();
if(mDatabase!= null && mDatabase.isOpen()){
return;
}
mDatabase = SQLiteDatabase.openDatabase(dbPath,null,SQLiteDatabase.OPEN_READWRITE);
}
public Cursor getItems(){
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables("Attraction");
Cursor c = qb.query(db,null,null,null,null,null,null);
c.moveToFirst();
return c;
}
}
My main
public class MainActivity extends AppCompatActivity {
DBHelper newDB;
private Cursor attraction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newDB = new DBHelper(this);
newDB.openDatabase();
attraction = newDB.getItems();
}
}
I am probably having a mental block and not seeing the solution but any more help on this issue will be appreciated.

Related Links

Doubles getting assigned to a List of Integer in java [duplicate]
How do I make windows applications using Java? [closed]
Check if annotation processing or usual runtime in Java
Mock inherited protected method
Using Hibernate to select the most common value
log4j is not logging entire message when its too long
which datasource api supports both connection pooling and XA transactions?
How to compose a Java file path [duplicate]
What happens after Jetty calls ServletContextListener.contextInitialized()
How do I generate random numbers for that array that don't repeat? (java)
Array outof bound Mapreduce
Trying to create a array with .split and null values after it java
Setting ArrayList items in one Class from another Class
Settings Relationship Properties in Neo4J OGM
java runtime.exec: Do not show window for user in case of crash
High Memory consumption for ArrayList object [closed]

Categories

HOME
sendgrid
arduino-uno
winforms
clips
gerrit
stock
react-redux
electronics
cakephp-2.5
android-youtube-api
virtualization
medical
vifm
midi
alignment
vb.net-2010
event-handling
flask-wtforms
workload-scheduler
telephony
footer
dbext
cultureinfo
blazemeter
firebase-crash-reporting
crystal-reports-2010
bootstrap-material-design
objectanimator
srcset
google-sites-2016
calibre
caret
hybridauth
web-mining
espeak
libvpx
vapor
bootstrapper
unoconv
s
multipeer-connectivity
return-value
aurelia-validation
nodebb
spring-mongodb
hittest
google-cdn
messenger
hexdump
yaws
goose
kendonumerictextbox
tcpserver
rtbkit
teamcity-8.0
kcachegrind
angular-leaflet-directive
cakephp-3.1
responsive-images
interrupted-exception
myo
ami
notify
rake-task
offloading
clicktag
typekit
relocation
javafx-webengine
asp.net-web-api-odata
wordpress-theme-customize
document-classification
uitouch
funcunit
hippomocks
cloud-connect
multiprocessor
ruby-datamapper
gwt-rpc
yui-compressor
limejs
isnullorempty
labwindows
tridion-worldserver
chrono
windows-phone-7.1.1
work-stealing
stage
data-loss
vdsp
yetanotherforum
mongomapper
blitz++
sortable-tables
infrastructure
meego
xetex
post-build
3gp
ajax-forms
misv
ntvdm.exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App