B4J Question Problems with AddNonQueryToBatch [SOLVED]

GuyBooth

Active Member
Licensed User
Longtime User
Several months ago I wrote and tested the code below with MariaDB 5 and mariadb-java-client-2.3.0. I am sure it was working.
Today I am using MariaDB10 and mariadb-java-client-2.4.4 and I am seeing nullpointerexceptions.
Here's what I know. The following code calls the routine that is giving me the problem:

B4X:
        ' Connect to the MusicBase
        dtmm_SQL.CP.GetConnectionAsync("sqlMB")
        wait for sqlMB_ConnectionReady(Connected As Boolean, sqlMB As SQL)
        If Connected Then
            ' Queue the Addrecording command
            Wait for (dtmm_SQL.Add_TableRecord(sqlMB, dtmm_SQL.cmdAddRecording, lstArgs, False)) Complete (Success As Boolean)
            ' Queue the AddExtRecording and execute both commands
            Wait for (dtmm_SQL.Add_TableRecord(sqlMB, dtmm_SQL.cmdAddExtRecording, lstExtArgs, True)) Complete (Success As Boolean)
            CDAdded = Success
        End If
        sqlMB.Close
The code where the error is thrown is n this sub:
B4X:
Public Sub Add_TableRecord(sqlMB As JdbcSQL, Query As String, lstArgs As List, LastInBatch As Boolean) As ResumableSub
    Log("Sub: Add_TableRecord")
    Dim RecordsAdded As Boolean
    Try   
        ' This line throws an error
        sqlMB.AddNonQueryToBatch(Query, lstArgs)
        
        If LastInBatch Then
            ' Run the batch asynchronously
            Wait For (sqlMB.ExecNonQueryBatch("sqlMB")) sqlMB_NonQueryComplete (Success As Boolean)
            RecordsAdded = Success
        End If
        ' Works
        ' sqlMB.ExecNonQuery2(Query, lstArgs)
    Catch
        dtmm.Log_Error(LastException.Message)
    End Try       
    Return RecordsAdded
End Sub
If I don't try to create a batch and instead use this direct instruction :
B4X:
        sqlMB.ExecNonQuery2(Query, lstArgs)
it works without errors (but there are reasons I want to batch it, and I have other places where I use similar code.).

The error log is as follows (removed the Try/Catch loop to give multiple lines of the log):
B4X:
Error occurred on line: 455 (dtmm_SQL)
java.lang.NullPointerException
    at anywheresoftware.b4j.objects.SQL.AddNonQueryToBatch(SQL.java:224)
    at b4j.DestopMusicMachine.dtmm_sql$ResumableSub_Add_TableRecord.resume(dtmm_sql.java:1203)
    at b4j.DestopMusicMachine.dtmm_sql._add_tablerecord(dtmm_sql.java:1164)
    at b4j.DestopMusicMachine.claddcd$ResumableSub_btnAddClose_Click.resume(claddcd.java:342)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
    at jdk.internal.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:42)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:136)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:85)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA$3.run(BA.java:247)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._enterNestedEventLoopImpl(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._enterNestedEventLoop(WinApplication.java:201)
    at javafx.graphics/com.sun.glass.ui.Application.enterNestedEventLoop(Application.java:509)
    at javafx.graphics/com.sun.glass.ui.EventLoop.enter(EventLoop.java:107)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(QuantumToolkit.java:591)
    at javafx.graphics/javafx.stage.Stage.showAndWait(Stage.java:465)
    at anywheresoftware.b4j.objects.Form.ShowAndWait(Form.java:215)
    at b4j.DestopMusicMachine.claddcd$ResumableSub_ShowMe.resume(claddcd.java:166)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
    at jdk.internal.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)

So essentially - my code refuses to create the batch.
Am I doing something wrong here, or is there something else going on?
 

GuyBooth

Active Member
Licensed User
Longtime User
Several months ago I wrote and tested the code below with MariaDB 5 and mariadb-java-client-2.3.0. I am sure it was working.
Today I am using MariaDB10 and mariadb-java-client-2.4.4 and I am seeing nullpointerexceptions.
''''
So essentially - my code refuses to create the batch.
Am I doing something wrong here, or is there something else going on?

At some point I added the jdbcSQL library and replaced all my "jSQL" object references with "jdbcSQL" objects. If I revert back to jSQL it all works properly - but shouldn't I be using the jdbcSQL library? Is there something wrong with it?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The error happens not in the batch or from the batch.

b4j.DestopMusicMachine.claddcd$ResumableSub_ShowMe.resume(claddcd.java:166)
The error is due to a
b4j.DestopMusicMachine.claddcd$ResumableSub_btnAddClose_Click.resume(claddcd.java:342)
check your code in claddcd at line 166 and 342.
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
The error happens not in the batch or from the batch.
The error is due to a
check your code in claddcd at line 166 and 342.
I don't believe it has anything to do with claddcd … to prove it, I pulled the relevant code and simplified it into a simple routine in Main, triggered by a single button click:
B4X:
Sub Button4_Click
    ' Adda parameter to the query
    Dim lstArgs As List
    lstArgs.Initialize
    lstArgs.Add("12345")
    Dim cmdAddRecording  As String= "INSERT INTO recordings SET SIGNATURE = ?;"
    ' Connect to the Music DataBase
    dtmm_SQL.CP.GetConnectionAsync("sqlMB")
    wait for sqlMB_ConnectionReady(Connected As Boolean, sqlMB As SQL)
    Log($"Connected is ${Connected}"$)
    If Connected Then
        sqlMB.AddNonQueryToBatch(cmdAddRecording, lstArgs)
        sqlMB.Close     
    End If
End Sub
All I am trying to do with this is add the request to the batch, not trying to execute it for now. I get the same NullPointerException error as before, with Line 1568 is the highlighted line in my code above

B4X:
Error occurred on line: 1568 (Main)
Connected is true
java.lang.NullPointerException
    at anywheresoftware.b4j.objects.SQL.AddNonQueryToBatch(SQL.java:224)
    at b4j.DestopMusicMachine.main$ResumableSub_Button4_Click.resume(main.java:1710)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
    at jdk.internal.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:42)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:136)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:85)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA$3.run(BA.java:247)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)
Anyone else seen this problem? Is it a bug in jdbcSQL? It runs fine if I use jSQL instead ….
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
You haven't initialized the SQL object.
Note that JdbcSQL is a B4A library. No reason to use it with B4J (though it will work).

I assumed that any initialization takes place when I initialize the connection pool:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private XUI As XUI
    Private MusicBase_IPAddress As String = "192.168.x.x"
    ' MariaDB 10:
    Private MusicBase_Port As String = "3307"
    Private MusicDatabase_Name As String = "<Database Name>"
    Private driver As String     = "org.mariadb.jdbc.Driver"
    Private jdbcUrl As String     = $"jdbc:mariadb://${MusicBase_IPAddress}:${MusicBase_Port}/${<Database Name>}"$
    Private Username As String     = "<User_Name>"
    Private Password As String     = "<Password>"
    Public CP As ConnectionPool
    '''
End sub

Public Sub Setup_ConnectionPool
    Log($"Sub: Setup ConnectionPool"$)
    CP.Initialize(driver, jdbcUrl, Username, Password)
End Sub
Then I call for the connection
B4X:
    dtmm_SQL.CP.GetConnectionAsync("sqlMB")
    wait for sqlMB_ConnectionReady(Connected As Boolean, sqlMB As SQL)
If this doesn't initialize the jDBCSQL object, where do I initialize it?
 
Upvote 0
Top