Android Question [SOLVED] jRDC2 Object should first be initialized (JavaObject)

josejad

Expert
Licensed User
Longtime User
Hi all:

I've been using jRDC2 for a while to access mysql with no problems. Now, I need to access another database in the same server, and I would like to use the same jRDC2 server.

I've seen in
https://www.b4x.com/android/forum/threads/jrdc2-connect-two-or-more-mysql-databases.98569/
you can connect differents databases just using 'db.table' in SQL.

My config.properties:
B4X:
#DATABASE CONFIGURATION
DriverClass=com.mysql.jdbc.Driver
JdbcUrl=jdbc:mysql://localhost:3306/semi?characterEncoding=utf8
User=root
Password=xxxxxxxx
#Java server port
ServerPort=8090
Accessing to semi database with no problem.
Now I want to access to 'peru' database in the same server.
So I'm using as sql
B4X:
sql.selectTasks = SELECT * FROM `peru.tasks`
but I'm getting
B4X:
(RuntimeException) java.lang.RuntimeException: Object should first be initialized (JavaObject).
Command: , took: 870ms, client=192.168.1.132
(MySQLSyntaxErrorException) com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'semi.peru.emplazamientos' doesn't exist
Command: , took: 31ms, client=192.168.1.132

I've seen in
https://www.b4x.com/android/forum/t...and-object-should-first-be-initialized.97512/
The OP had a problem with a field in the database, but I've not added any new field. I had a table called "task" in my original semi database, so I've deleted it to see if it was the problem.
Watching the error ('semi.peru.emplazamientos' doesn't exist) I've changed my config.properties to:
B4X:
JdbcUrl=jdbc:mysql://localhost:3306/peru?characterEncoding=utf8
and sql command to
B4X:
sql.selectTasks = SELECT * FROM `tasks`

But then I get just the two first lines of error (Object should first be initialized).
Watching the code in RDCHandler, I think the problem must be "rs" is getting no value or something like that.
B4X:
    Dim rs As ResultSet = con.ExecQuery2(Main.rdcConnector1.GetCommand(cmd.Name), cmd.Parameters)
    If limit <= 0 Then limit = 0x7fffffff 'max int
    Dim jrs As JavaObject = rs
    Dim rsmd As JavaObject = jrs.RunMethod("getMetaData", Null)

Any help is welcome.

Thanks in advance
 

OliverA

Expert
Licensed User
Longtime User
It needs to be
B4X:
JdbcUrl=jdbc:mysql://localhost:3306?characterEncoding=utf8
That coonects you to the root of the dB. Now all your queries must preface the table names with the appropriate database name
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Hi OliverA:

Thanks for the tip. I've tried this but with the slash following 3306,
B4X:
JdbcUrl=jdbc:mysql://localhost:3306/?characterEncoding=utf8

Anyway, it's not working. I've seen one more thing, if I choose a command from the database semi, it works:
B4X:
Dim cmd As DBCommand = CreateCommand("selectSiteName", Array(id))

'config.properties
sql.selectSiteName = SELECT `NOMBRE` FROM semi.emplazamientos WHERE `ID_SITE`=?
And I get in the B4J log:
B4X:
Command: query: selectSiteName, took: 622ms, client=192.168.1.149

But if I use a command to database peru
B4X:
Dim cmd As DBCommand = CreateCommand("selectPeruTarea", Array(id))

'config.properties
sql.selectPeruTarea = SELECT * FROM peru.tareas WHERE `id` = ?
I get in the B4J log:
B4X:
(RuntimeException) java.lang.RuntimeException: Object should first be initialized (JavaObject).
Command: , took: 8ms, client=192.168.1.149
I don't get the query name. Probably this is the problem, but I don't know why it's happening.
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I don't get the query name. Probably this is the problem, but I don't know why it's happening.
If jRDC2 cannot find selectPeruTarea, it would log "*** Command not found: select selectPeruTarea". Have you tried executing jRDC2 in Debug mode to see if the logs are more verbose?
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
I'm executing jRDC2 in Debug mode, but that's all I get from Logs.
I've tried:

- In B4A added in CreateCommand
B4X:
Sub CreateCommand(Name As String, Parameters() As Object) As DBCommand
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = Name
    Log("command: " & Name) '**** ADDED THIS LINE
    If Parameters <> Null Then cmd.Parameters = Parameters
    Return cmd
End Sub

Ok, I get: "command: selectPeruTarea" in Logs (Testing if B4A is not rigth sending the command)

- In B4J, in Handle sub added some logs
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim start As Long = DateTime.Now
    Dim q As String 
    Dim in As InputStream = req.InputStream
    Dim method As String = req.GetParameter("method")
    Log("Method: " & method) '********* ADDED THIS LINE
    Dim con As SQL
    Try
       
        con = Main.rdcConnector1.GetConnection
        If method = "query2" Then
            q = ExecuteQuery2(con, in, resp)
            Log("q in query2: " & q) '********* ADDED THIS LINE
            ...
            ...
            ...
        Else if method = "batch2" Then
            q = ExecuteBatch2(con, in, resp)
            Log("q in batch2: " & q) '********* ADDED THIS LINE
        Else
            Log("Unknown method: " & method)
            resp.SendError(500, "unknown method")
        End If
    Catch
        Log(LastException)
        resp.SendError(500, LastException.Message)
    End Try
    If con <> Null And con.IsInitialized Then con.Close
    Log($"Command: ${q}, took: ${DateTime.Now - start}ms, client=${req.RemoteAddress}"$)
End Sub

And in sub ExecuteQuery2:
B4X:
Private Sub ExecuteQuery2 (con As SQL, in As InputStream,  resp As ServletResponse) As String
    Log("Entering ExecuteQuery2")  
    ...
    ...
    ...
    Log("Query in ExecuteQuery2:  " & cmd.Name)
    Return "query: " & cmd.Name '********* ADDED THIS LINE
End Sub

The only thing I get in the B4J Log is:
B4X:
Method: query2
Entering ExecuteQuery2
(RuntimeException) java.lang.RuntimeException: Object should first be initialized (JavaObject).
Command: , took: 5ms, client=192.168.1.149

With another command to semi db, it's working, but with this command, there's a problem in ExecuteQuery2. I've tried to change the command name... same result
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
In ExecuteQuery2, add these logs:
B4X:
    Log("Before getting meta  data")
    Dim rsmd As JavaObject = jrs.RunMethod("getMetaData", Null)
    Log("After getting meta data")
    '... then further down
    Log("Before getting column type")
    Dim ct As Int = rsmd.RunMethod("getColumnType", Array(i + 1))
    Log("After getting column type")
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
This is what I get:

B4X:
2019-02-06 17:03:18.435:INFO:oejs.AbstractConnector:main: Started ServerConnector@371a67ec{HTTP/1.1,[http/1.1]}{0.0.0.0:8090}
2019-02-06 17:03:18.435:INFO:oejs.Server:main: Started @1838ms
Emulated network latency: 100ms
jRDC is running (version = 2.21)
Method: query2
feb 06, 2019 5:04:37 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource
INFORMACIÓN: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 20000, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1hge17aa01i9ab8o6wsxdl|7006c658, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1hge17aa01i9ab8o6wsxdl|7006c658, idleConnectionTestPeriod -> 600, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306?characterEncoding=utf8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 1800, maxI...
Entering ExecuteQuery2
Before getting meta  data
After getting meta data
Before getting column type
After getting column type
Before getting column type
After getting column type
Before getting column type
After getting column type
Before getting column type
After getting column type
Before getting column type
After getting column type
Before getting column type
After getting column type
Before getting column type
(RuntimeException) java.lang.RuntimeException: Object should first be initialized (JavaObject).
Command: , took: 734ms, client=192.168.1.149
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Huh? What happened to the Exception?
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Ups, sorry, it seems not all the lines were pasted. I've updated the last post... Exception is still there... :-(
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Ok that is strange. According to this,
B4X:
Dim ct As Int = rsmd.RunMethod("getColumnType", Array(i + 1))
works perfectly several times and then suddenly blows up. It seems like rmsd suddenly becomes an uninitialized JavaObject. At first glance, how is this even possible? How many columns should the query return? What is the 8th column? Try this (so we know at what column this seems to choke up):
B4X:
    Log("Before getting column type")
    Log("Column: " & rs.GetColumnName(i))
    Dim ct As Int = rsmd.RunMethod("getColumnType", Array(i + 1))
    Log("After getting column type")
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
First of all, thanks for your help and your patience.

I've commented the Log "before..." and "after...", first time I run with the new Log("Column: ...") I got a red error in the logs with the "Object should first be...", but after that error is gone.

B4X:
Entering ExecuteQuery2
Before getting meta  data
After getting meta data
Column: id
Column: descripcion
Column: fecha
Column: asignada_a
Column: completada
Column: fecha_fin
Column: observaciones
(RuntimeException) java.lang.RuntimeException: Object should first be initialized (JavaObject).
Command: , took: 601ms, client=192.168.1.149

If tried deleting the last column (observaciones), but same error
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
What is fecha_fin? How is it declared in your dB? It is causing the issue, since after it rsmd seems to be uninitialized
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
This is my db declaration:
B4X:
    CREATE TABLE `tareas` (
  `id` int(11) NOT NULL,
  `descripcion` varchar(50) COLLATE utf8_spanish_ci NOT NULL,
  `fecha` date NOT NULL,
  `asignada` varchar(9) COLLATE utf8_spanish_ci NOT NULL,
  `completada` enum('SI','NO') COLLATE utf8_spanish_ci NOT NULL,
  `fecha_fin` date DEFAULT NULL,
  `observaciones` text COLLATE utf8_spanish_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;

fecha_fin is a date field. I've tried to change the name to see if underscore was causing the problem (I changed "asignada_a" to "asignada" before this). Same error.

But when I've deleted the field... AND IT WORKS¡¡¡

B4X:
Query in ExecuteQuery2:  selectPeruTarea
q in query2: query: selectPeruTarea
Command: query: selectPeruTarea, took: 58ms, client=192.168.1.132

How did you know it was the problematic field if all of them were showed?

I don't know if mark the thread as SOLVED or wait if someone knows why the problem is caused. After the test, I've added again a date field and the error appears again. But I have date fields in my other db and it works without problems

Thanks again¡¡
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
How did you know it was the problematic field if all of them were showed?
1) Dates can be problematic and 2) the error happened right after the the result for the Date field was pulled (I guessed that _fin would indicate a date field)
Try this (with your date field back in the table).
B4X:
           For i = 0 To cols - 1
               Log("Column: " & rs.GetColumnName(i))
               Dim ct As Int = rsmd.RunMethod("getColumnType", Array(i + 1))
               Log("Column Type: " & ct)
               'check whether it is a blob field
               If ct = -2 Or ct = 2004 Or ct = -3 Or ct = -4 Then
                   Log("-2, 2004, -3, -4")
                   row(i) = rs.GetBlob2(i)
                   Log("rsmd initialized? " & rsmd.IsInitialized)
                   Log("jrs initialized? " & jrs.IsInitialized)
               Else if ct = 2 Or ct = 3 Then
                   Log("2, 3")
                   row(i) = rs.GetDouble2(i)
                   Log("rsmd initialized? " & rsmd.IsInitialized)
                   Log("jrs initialized? " & jrs.IsInitialized)
               Else If DateTimeMethods.ContainsKey(ct) Then
                   Log("Date/Time Field")
                   row(i) = jrs.RunMethodJO(DateTimeMethods.Get(ct), Array(i + 1)).RunMethod("getTime", Null)
                   Log("rsmd initialized? " & rsmd.IsInitialized)
                   Log("jrs initialized? " & jrs.IsInitialized)
               Else
                   Log("Everything Else")
                   row(i) = jrs.RunMethod("getObject", Array(i + 1))
                   Log("rsmd initialized? " & rsmd.IsInitialized)
                   Log("jrs initialized? " & jrs.IsInitialized)
               End If
           Next
Note: This depends on the latest jRDC2 that has some extra date handling functionality (the Else If DateTimeMethods.ContainsKey(ct) Then code). If you don't have the latest code, than that may be your issue (since the latest code has the date handing functionality)
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
I've downloaded the last versions of DBRequestManager, and the jRDC2 server from the main thread:

https://www.b4x.com/android/forum/t...ation-of-rdc-remote-database-connector.61801/

I see no differences with the one I got.
B4X:
jRDC is running (version = 2.21)

Private VERSION As Float = 2 'DBRequestManager

The log with your code, now the date field is called "fin"
(I guessed that _fin would indicate a date field)
The name of the field was "fecha_fin". "fecha" is date and "fin" is end :) So it was something like "end_date". The type is: 91
B4X:
Column: id
Column Type: 4
Everything Else
rsmd initialized? true
jrs initialized? true
Column: descripcion
Column Type: 12
Everything Else
rsmd initialized? true
jrs initialized? true
Column: fecha
Column Type: 91
Date/Time Field
rsmd initialized? true
jrs initialized? true
Column: asignada
Column Type: 12
Everything Else
rsmd initialized? true
jrs initialized? true
Column: completada
Column Type: 1
Everything Else
rsmd initialized? true
jrs initialized? true
Column: fin
Column Type: 91
Date/Time Field
(RuntimeException) java.lang.RuntimeException: Object should first be initialized (JavaObject).
Command: , took: 680ms, client=192.168.1.132

The problem is when the field is NULL¡¡ If I fill the field with a date, I got the right result¡¡
B4X:
Command: query: selectPeruTarea, took: 10ms, client=192.168.1.132
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
So the exception occurs in this line
B4X:
row(i) = jrs.RunMethodJO(DateTimeMethods.Get(ct), Array(i + 1)).RunMethod("getTime", Null)
Could a NULL date cause this to trip up? I noticed that fecha does not allow NULLs, but fecha_fin does.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

josejad

Expert
Licensed User
Longtime User
:( I'm so sorry for having wasted your time.
Sorry, when I was looking for the error, the forum search doesn't return that thread to me.

My apologies, and.. again... THANKS
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
The only reason I found that post is because I searched for DateTimeMethods.ContainsKey(ct). Something started nagging me (this issue seemed really familiar).
 
Upvote 0
Top