Android Question DBUtils NullPointerException when not using breaks

juacochero

Member
Licensed User
Hi everyone!
Ok, I keep getting this problem. I have a SQLite (edited) database in my project, and I use DBUtils (latest version) to communicate with it.
If I run the code normally (pressing f5, even in Release mode), I get an error.
But if I run it using breaks every couple of lines, or even step-by-step, no errors occur and everything works fine.
It happens almost every time I want to interact with the database, I will post an example:

MY CODE:

B4X:
    'LOADS ALL RESULTS TO A TABLE
    Dim Table As List
    Table.Initialize
   
    Table = DBUtils.ExecuteMemoryTable(Starter.sqlDB, "SELECT * FROM evals WHERE usuario='" & Main.strUserName & "'", Null, 0)
   
    If Table = Null Or Table.IsInitialized = False Then
         'DOES NOTHING FOR NOW
    Else
        Dim Cols() As String
        For dato = 0 To Table.Size - 1
            Cols = Table.Get(dato)
            'CHECKS A SPECIFIC COLUMN FOR A 'YES' VALUE. If so, it adds a line to a 2 line listview
            If Cols(40) = "YES" Then
                ListView1.AddTwoLinesAndBitmap2(Cols(4),Cols(5),LoadBitmap(File.DirAssets, "check.png"),Cols(0))
            End If
        Next
   
    End If
THE ERROR:
B4X:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    at cochero.appear.frmdatosanteriores._listardatos(frmdatosanteriores.java:536)
    at cochero.appear.frmdatosanteriores._activity_resume(frmdatosanteriores.java:448)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at cochero.appear.frmdatosanteriores.afterFirstLayout(frmdatosanteriores.java:108)
    at cochero.appear.frmdatosanteriores.access$000(frmdatosanteriores.java:17)
    at cochero.appear.frmdatosanteriores$WaitForLayout.run(frmdatosanteriores.java:80)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5451)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 
Last edited:

Erel

Administrator
Staff member
Licensed User
1. No reason to Initialize the list (Table) before you set it anyway.

2. Bad code:
B4X:
    Table = DBUtils.ExecuteMemoryTable(Starter.sqlDB, "SELECT * FROM evals WHERE usuario='" & Main.strUserName & "'", Null, 0)
Good code:
B4X:
    Table = DBUtils.ExecuteMemoryTable(Starter.sqlDB, "SELECT * FROM evals WHERE usuario=?", Array As String(Main.strUserName), 0)
3. You should run in debug mode and see which line raises the error.

My guess is that your code fails when Cols(40) is Null. You can exclude these results:
B4X:
Table = DBUtils.ExecuteMemoryTable(Starter.sqlDB, "SELECT * FROM evals WHERE usuario=? AND NOT ReleventColumn IS NULL", Array As String(Main.strUserName), 0)
 

juacochero

Member
Licensed User
1. No reason to Initialize the list (Table) before you set it anyway.

2. Bad code:
B4X:
    Table = DBUtils.ExecuteMemoryTable(Starter.sqlDB, "SELECT * FROM evals WHERE usuario='" & Main.strUserName & "'", Null, 0)
Good code:
B4X:
    Table = DBUtils.ExecuteMemoryTable(Starter.sqlDB, "SELECT * FROM evals WHERE usuario=?", Array As String(Main.strUserName), 0)
3. You should run in debug mode and see which line raises the error.

My guess is that your code fails when Cols(40) is Null. You can exclude these results:
B4X:
Table = DBUtils.ExecuteMemoryTable(Starter.sqlDB, "SELECT * FROM evals WHERE usuario=? AND NOT ReleventColumn IS NULL", Array As String(Main.strUserName), 0)
Thank you so much, as usual, Erel.
I removed the initialization line for the Table, and changed the "Bad code" for the "Good code", and it works like a charm.
So apparently it has to do on how I pass the variable in the query. The weird part was that if I ran it in debug mode and line by line the error didn't appear.

Again, Thank you so much, specially for answering back on the 1st day of the year! You should be resting!!!
Cheers!
 
Top