1
votes

I have written an application using CN1 that access an sqlite db. When I run the app in the emulator using Netbeans it works perfectly. However, when I try to connect to the db on the actual device I get this error ...

java,lang,NullPointerException: Attempt to invoke virtual method 'void.com.codename1.db.Database.close()' on a null object reference

I have been through my code to try and see if I am doing a 'close' before I have even created the DB instance and I cannot see it - and if I were doing this then I would expect it to fail in the simulator right?

Any help is appreciated

Thanks

2
Which code do you use to open the database? The path in the logs is clearly wrong - Shai Almog
I am not sure how this works - so here is a link to my sample code ... docs.google.com/document/d/… --- even if it were incorrect, It wouldn't work in the emulator right? - Steven Mark Integration
So I have just been debugging my code and at one point I can see the following values: conn.url=jdbc:sqlite:/home/stevenmarkintegration/.cn1/database/MyDB.db and conn.filename=/home/stevenmarkintegration/.cn1/database/MyDB.db .... now those paths wouldn't exist on my phone - could that be the reason - or am I way off? - Steven Mark Integration

2 Answers

1
votes

In your code you do:

dbPath = Display.getInstance().getDatabasePath("MyDB.db");
if(dbPath != null && !FileSystemStorage.getInstance().exists(dbPath)) {
        copyDb(dbPath);
    }
}

Then you do:

db = Display.getInstance().openOrCreate(dbPath);

Which is a mistake and I'm a bit surprised this works in the simulator. You need to use:

db = Display.getInstance().openOrCreate("MyDB.db");

Original answer below:

If you connect your Android device with a cable and look through DDMS You should be able to see the full stack trace of the error that would point you to the specific line of code that is failing.

Notice that this should also work fine with crash protection without the need to of DDMS or a cable for this case.

0
votes

I managed to find the cause of the problem using the "adb" command following this YouTube tutorial: https://www.youtube.com/watch?v=3wMlCucwGvE

When I followed these instructions I could see the following output

10-27 08:14:12.580  3967  3986 E SQLiteDatabase: Failed to open database '/databases/MyDB.db'.

10-27 08:14:12.580  3967  3986 E SQLiteDatabase: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 1294): Could not open database

10-27 08:14:12.580  3967  3986 E SQLiteDatabase: #################################################################

10-27 08:14:12.580  3967  3986 E SQLiteDatabase: Error Code : 1294 (SQLITE_CANTOPEN_ENOENT)

10-27 08:14:12.580  3967  3986 E SQLiteDatabase: Caused By : Specified directory or database file does not exist.

10-27 08:14:12.580  3967  3986 E SQLiteDatabase:    (unknown error (code 1294): Could not open database)

10-27 08:14:12.580  3967  3986 E SQLiteDatabase:        #################################################################

So this output - together with my original error message is telling me that I am trying to close the db before I have opened it.

So this raises two questions in my mind:

1] Howcome it works perfectly in the emulator?

2] Do I need to check if a db is open before I try and close it?