B4J Question SD_SQL error

ivanomonti

Expert
Licensed User
Longtime User
I have this error that the SD_SQL library gives me and I don't understand since the query on MYSQL WorkBench the record is generated to me, i think its a datatime issue i pass as string or i wouldn't know

1679936445450.png


table mysql:
CREATE TABLE `tb01` (
  `ms_gpt_01_01` varchar(45) NOT NULL,
  `ms_gpt_01_02` varchar(45) DEFAULT NULL COMMENT 'ip client',
  `ms_gpt_01_03` varchar(45) DEFAULT NULL COMMENT 'id client',
  `ms_gpt_01_04` longtext COMMENT 'question',
  `ms_gpt_01_05` longtext COMMENT 'aswer question',
  `ms_gpt_01_06` datetime DEFAULT NULL,
  `ms_gpt_01_07` varchar(45) NOT NULL DEFAULT 'False'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='tabella request';

B4X:
' datetime mysql YYYY-MM-DD HH:MI:SS
       
        DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss"
        Client_dataQuestion = DateTime.Date(DateTime.Now)
       
        Dim query As String
        query = "INSERT INTO `ms_gpt`.`tb01`" & "(`ms_gpt_01_01`," & "`ms_gpt_01_02`," & "`ms_gpt_01_03`," &  "`ms_gpt_01_04`," & "`ms_gpt_01_05`," & "`ms_gpt_01_06`," & "`ms_gpt_01_07`)" & "VALUES" & _
            "('" & Client_id & "-"&  Rnd(100,9999) & "'," & _
            "'" & Client_ip & "'," & _
            "'" & Client_id &  "'," & _
            "'" & Client_question & "'," & _
            "'" & Client_aswerQuestion & "'," & _
            "'" & Client_dataQuestion & "'," & _
            "'" & Client_statusQuestion & "');"
       
        If MySql.IsInitialized = False Then
            initializeMySQL
        End If
       
        Try
            If MySql.ExecNonQuery(query) = False Then
                MySql.Close
            End If
        Catch
            MySql.Close
            Log(LastException)
        End Try

Query:
INSERT INTO `ms_gpt`.`tb01`(`ms_gpt_01_01`,`ms_gpt_01_02`,`ms_gpt_01_03`,`ms_gpt_01_04`,`ms_gpt_01_05`,`ms_gpt_01_06`,`ms_gpt_01_07`)VALUES('12345-1287','192.168.1.41','12345','chi era napoleone?','....','2023-03-27 18:50:52','false');

Error:
Waiting for debugger to connect...
Program started.
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
connected
java.lang.NullPointerException
    at b4j.example.sd_sql.close(sd_sql.java:303)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at b4j.example.sd_sql._close(sd_sql.java:92)
    at b4j.example.b4xmainpage._button_sub_click(b4xmainpage.java:136)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    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:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    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:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA$1.run(BA.java:236)
    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)
Errore di query
(NullPointerException) java.lang.NullPointerException
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
After initialized it is not said that the connection was successful. You may have got the wrong parameters or there is no connection at that moment or whatever else has gone wrong.

Before a query you should always verify that the connection initiated by the initialization was successful. Otherwise it doesn't run the query returning FALSE and closing the connection generates error because the connection doesn't exist and it can't close the one that doesn't exist
 
Upvote 0

ivanomonti

Expert
Licensed User
Longtime User
in the posted code if you notice I check the connection and if false I initialize SD_SQL, it gives me error only when I insert new record, and since I inserted the datetime in mysql, but the record writes it then gives me an error.

SD_MQL:
 If MySql.IsInitialized = False Then
            initializeMySQL
        End If


B4X:
Sub initializeMySQL
    MySql.Initialize(Me,"MYSQL", "com.mysql.jdbc.Driver", $"jdbc:mysql://${MyLocation}/${MyDatabase}?useSSL=false"$,  MyUsername, MyPassword)
End Sub
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
You did not understand what I wrote to you and confuse the check if it is initialized with the check if connected.

However the error indicates that one of the internal connection objects is not initialized. I repeat: Initialization may have failed to connect, or the query/operation was not performed and thus did not create/initialize the objects. You don't need to close the connection if you haven't run the command.

The message you received is a warning and should have no effect on the program. I'll remove the message so no one gets into a crisis
 
Last edited:
Upvote 0

ivanomonti

Expert
Licensed User
Longtime User
You did not understand what I wrote to you and confuse the check if it is initialized with the check if connected.

However the error indicates that one of the internal connection objects is not initialized. I repeat: Initialization may have failed to connect, or the query/operation was not performed and thus did not create/initialize the objects. You don't need to close the connection if you haven't run the command.

The message you received is a warning and should have no effect on the program. I'll remove the message so no one gets into a crisis

I understand what you are saying but the error crashes the program
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Try release 0.05
 
Upvote 0

ivanomonti

Expert
Licensed User
Longtime User
Try release 0.05
It only happens to me on insert at the moment, on select it doesn't happen but I assume it will happen on update as well. The record is however registered on mysql, the problem that I skip all the code inside if

B4X:
Private Sub Button_register_Click
   
    Dim bt As Button = Sender
   
    If bt.Tag = "button-register-open-login" Then ' apre la finestra login
        Root.RemoveAllViews
        Root.LoadLayout("frame_login")
    End If
   
    If bt.Tag = "button-register-save" Then 'registra il nuovo profilo
       
        If TextField_mail.Text="" Or TextField_password.Text = "" Or TextField_name.Text = "" Or TextField_surname.Text = "" Or TextField_phone.Text = "" Then
            fx.Msgbox(Main.MainForm,"fill in the email and password, name e surname and phone fields","Alert Login")
            Return
        End If
       
        Dim query As String =     "INSERT INTO `ms_gpt`.`tb00`" & _
        "(`ms_01`," & _
        "`ms_02`, " & _
        "`ms_03`," & _
        "`ms_04`," & _
        "`ms_05`," & _
        "`ms_06`," & _
        "`ms_07`," & _
        "`ms_08`)" & _
        "VALUES " & _
        "('" & TextField_name.Text &"'," & _
        "'" & TextField_surname.Text & "'," & _
        "'" & TextField_mail.Text & "'," & _
        "'" & TextField_password.Text & "'," & _
        "'" & TextField_phone.Text & "'," & _
        "'null'," & _
        "'"& TextArea_info_profile.Text &"'," & _
        "'true');"
       
        Log(query)
       
        If MySql.IsInitialized = False Then
            initializeMySQL
        End If
       
        Try
            If MySql.ExecNonQuery(query) = False Then
                Root.RemoveAllViews
                Root.LoadLayout("frame_login")
            Else
                fx.Msgbox(Null,"registration not completed","Alert new Profile")
            End If
        Catch
            Log(LastException)
        End Try

        MySql.Close
       
    End If
   
    If bt.Tag = "button-register-rule" Then ' apre la finestra rule (info servizio) da farsi
        fx.Msgbox(Null, "Bottone ancora da definire","Alert developer")
        Return
        Root.RemoveAllViews
        Root.LoadLayout("frame_login")
    End If
   
End Sub


B4X:
INSERT INTO `ms_gpt`.`tb00`(`ms_01`,`ms_02`, `ms_03`,`ms_04`,`ms_05`,`ms_06`,`ms_07`,`ms_08`)VALUES ('bbbb','bb','bb','bb','bb','null','bb','true');
Error occurred on line: 53 (SD_SQL)
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at b4j.example.sd_sql._execnonquery(sd_sql.java:96)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    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:629)
    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:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA$1.run(BA.java:236)
    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)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.Util.getInstance(Util.java:408)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861)
    at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1192)
    at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1187)
    at com.mysql.jdbc.ConnectionImpl.createStatement(ConnectionImpl.java:2382)
    at com.mysql.jdbc.ConnectionImpl.createStatement(ConnectionImpl.java:2366)
    at b4j.example.sd_sql.execnonquery(sd_sql.java:286)
    ... 26 more
 
Upvote 0

teddybear

Well-Known Member
Licensed User
B4X:
  ...
        Dim query As String =     "INSERT INTO `ms_gpt`.`tb00`" & _
        "(`ms_01`," & _
        "`ms_02`, " & _
        "`ms_03`," & _
        "`ms_04`," & _
        "`ms_05`," & _
        "`ms_06`," & _
        "`ms_07`," & _
        "`ms_08`)" & _
Why do you define the columns and table name using back quotes( ` ) instead of single quotes( ' )?
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Try version 0.06 maybe I understood the problrma
 
Upvote 0

ivanomonti

Expert
Licensed User
Longtime User
Try version 0.06 maybe I understood the problrma
I solved the problem that had a persistence by creating a mysql class only, in this way I instantiate the class on the fly and execute it so at the end of its use it dies, while before it was a simple Main variable.

I hope I haven't pissed you off, now doing simultaneous operations they don't conflict, which was most likely the problem

I can now do multiple queries at the same time in the same app while reading, writing and updating records with a robot and everything runs smoothly
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I hope I haven't pissed you off
I don't know the code you wrote so it's difficult for me to understand why it throws you a certain error and having developed over a hundred libraries I certainly can't follow all the requests I receive in public and in private.
Many simply make programming errors it would be really difficult for me to help them especially in those cases where they can't understand what you are saying.
I already have a lot to do with my work

I solved the problem that had a persistence by creating a mysql class only, in this way I instantiate the class on the fly and execute it so at the end of its use it dies, while before it was a simple Main variable.
The SD_SQL library accesses only the underlying native libraries (jar to be clear) and makes the methods available without actually wrapping them.

If the underlying libraries are designed to work Asynchronous so will my library calling them. If your libraries don't support asynchronous operation you won't be able to work with multiple queries and operations at the same time, and it's not a timer thing like you speculated in another thread.

You were right to use different variables which allow each one to have connection to the database and can work independently from each other.
 
Upvote 0

ivanomonti

Expert
Licensed User
Longtime User
I don't know the code you wrote so it's difficult for me to understand why it throws you a certain error and having developed over a hundred libraries I certainly can't follow all the requests I receive in public and in private.
Many simply make programming errors it would be really difficult for me to help them especially in those cases where they can't understand what you are saying.
I already have a lot to do with my work


The SD_SQL library accesses only the underlying native libraries (jar to be clear) and makes the methods available without actually wrapping them.

If the underlying libraries are designed to work Asynchronous so will my library calling them. If your libraries don't support asynchronous operation you won't be able to work with multiple queries and operations at the same time, and it's not a timer thing like you speculated in another thread.

You were right to use different variables which allow each one to have connection to the database and can work independently from each other.

Sorry for my English :-(
 
Upvote 0
Top