Android Question How to make SQL object reusable

SimonCarr

Member
Licensed User
I am using SQLITE with B4A. I am using the database in multiple activities. I have created a service to contain my database object. StartAtBoot is set to True.

sqlManager
B4X:
#Region  Service Attributes
#StartAtBoot: True
    
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public sql As SQL
End Sub

Sub Service_Create
    
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    sql.Initialize(File.DirInternal,"ionpwsql",True)
End Sub

Sub Service_Destroy

End Sub
In my main module, I also try to start the service in my Activity_Create method

Main
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    
    Dim tabHostExtra As TabHostExtras
    StartService(sqlManager)
    If DBUtils.TableExists(sqlManager.sql, "category") = False Then
        CreateTables
    End If
    Activity.LoadLayout("main")
    appTabs.AddTab("Generate", "createPassword")
    appTabs.AddTab("Passwords", "passwordList")
    appTabs.AddTab("Settings", "settings")
    tabHostExtra.setTabTextSize(appTabs, 8.0)
    tabHostExtra.setTabHeight(appTabs, 50)
    
    For i = 1 To 20
        Dim pd As PasswordData
        pd.Initialize
        pd.Category = "Category " & i
        pd.Name = "Name " & i
        CustomListView1.Add(CreateItem(pd), pd)
    Next
    
End Sub

But when I try to run the application, I get the following error saying the Object should first be initialized.

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 487 (DBUtils)
java.lang.RuntimeException: Object should first be initialized.
    at anywheresoftware.b4a.sql.SQL.checkNull(SQL.java:55)
    at anywheresoftware.b4a.sql.SQL.ExecQuerySingleResult2(SQL.java:244)
    at b4a.example.dbutils._tableexists(dbutils.java:38)
    at b4a.example.main._activity_create(main.java:484)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at b4a.example.main.afterFirstLayout(main.java:104)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
** Activity (main) Resume **
 
Top