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

JPA - My Named Query won't work
Cascaded combo boxes java
Android Vertical Switch Widget
Getting GoogleApiClient to work with Activity Recognition
Android structuring / naming of classes
How to program behavior of a void method with Mockito? [duplicate]
Couldn't able to read the attribute using DOM parser
Java string comparison with object string
Multiple buttons. Passing actions to the next button in Java
Autowire a Spring service in JUnit test
JBoss AS 7 and MySQL - Can't start server
Java - JPanel vs Canvas
Java: How to remove duplicate string array from ArrayList?
Spring Boot test class wont inject beans
Using unique variable names in Xtend loop (Code Generation)
Using Scanner.in with java Applet

Categories

HOME
maven
cakephp
compiler-construction
image
wso2
pypi
magnific-popup
nullpointerexception
mean-stack
programming-languages
blueprintjs
fancybox-3
fortumo
clearcase-ucm
jsdoc
google-static-maps
google-cloud-speech
nodatime
conemu
xlsxwriter
mmap
java-7
h2db
uninstall
ejabberd-module
jaxb2
procdump
skia
winrt-xaml-toolkit
avcapturesession
git-merge
dartium
withings
starteam
particles.js
mapzen
botbuilder
android-mediaprojection
spring-security-kerberos
sas-jmp
babel-core
logfiles
jsch
scrollable
zip4j
react-scripts
ionicons
datastax-startup
git-diff
mplayer
sqlclient
capacity
zendesk-app
query-performance
watchconnectivity
py2app
django-debug-toolbar
plottable.js
qpid
connect-by
skype4py
python-stackless
ipconfig
freelancer.com-api
tcpserver
spim
cartesian-product
google-places
eclipse-clp
wireshark-dissector
embedded-code
npapi
python-green
gwidgets
dd
qpainter
tt-news
device-manager
bitcoinj
has-many-through
heisenbug
navigationservice
comexception
rtmfp
ivyde
sitemesh
mylyn
seed
simba
hamiltonian-cycle
propertyeditor
jquery-ui-layout
hirefire
mod-auth
subgurim-maps
yetanotherforum
gallio
sproutcore-2
preference
brewmp
xetex
spec#

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