Android Question jrdc2 no result mysql select

Milan Bozic

Member
Licensed User
Hello
Just tested simple android client with jrdc2 similarly to Erel's example
Creating a table works; inserting records works; select doesn't return anything

this is config file for jrdc2 server:

B4X:
DriverClass=com.mysql.jdbc.Driver
JdbcUrl=jdbc:mysql://localhost/mytest?characterEncoding=utf8
User=root
Password=
ServerPort=17178
#Debug=true

#commands
sql.create_table=CREATE TABLE if NOT EXISTS `animals` (`id` INT(11) NOT NULL auto_increment, `name` varchar(30), `image` blob, PRIMARY KEY (`id`))
sql.insert_animal=INSERT into animals VALUES (null, ?,?)
sql.select_animals=SELECT * FROM animals
sql.select_one_animal=SELECT * FROM animals where id = ?

This is code on b4a:

B4X:
Sub Process_Globals
Private const rdcLink As String = "http://192.168.58.94:17178/rdc"
Type DBResult (Tag As Object,columns As Map, rows As List)
Type DBCommand (Name As String, Parameters() As Object)
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub CreateRequest As DBRequestManager
    Dim req As DBRequestManager
    req.Initialize(Me, rdcLink)
    Return req
End Sub

Sub CreateCommand(Name As String, Parameters() As Object) As DBCommand
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name=Name
    If Parameters <> Null Then cmd.Parameters = Parameters
    Return cmd
End Sub

Sub CreateTable
    Dim cmd As DBCommand = CreateCommand("create_table", Null)
    Dim j As HttpJob = CreateRequest.ExecuteBatch(Array(cmd),Null)
    wait For(j) JobDone(j As HttpJob)
    If j.Success Then
        Log("Inserted successfully")
    End If
    j.release
End Sub

Sub InsertRecord (Name As String)
    Dim cmd As DBCommand = CreateCommand("insert_animal",Array(Name,Null))
    Dim j As HttpJob = CreateRequest.ExecuteBatch(Array(cmd),Null)
    wait For(j) JobDone(j As HttpJob)
    If j.Success Then
        Log("Inserted successfully")
        End If
    j.release
End Sub

Sub GetOneRecord(idt As Int)
    Dim req As DBRequestManager = CreateRequest
    Dim cmd As DBCommand = CreateCommand("select_one_animal", Array(idt))
    wait for (req.ExecuteQuery(cmd,0,Null)) JobDone(j As HttpJob)
    If j.success Then
        req.HandleJobAsync(j,"req")
        wait for (req) req_Result(res As DBResult)
        Log("done one record")
        'req.PrintTable(res)
    Else
        Log("ERROR: " & j.ErrorMessage)
    End If
    j.Release
    
End Sub
Sub GetRecord
    Dim req As DBRequestManager = CreateRequest
    Dim cmd As DBCommand = CreateCommand("select_animals", Null)
    wait for (req.ExecuteQuery(cmd,0,Null)) JobDone(j As HttpJob)
    If j.success Then
        req.HandleJobAsync(j,"req")
        wait for (req) req_Result(res As DBResult)
        Log("Done records")
        'req.PrintTable(res)
        Else
            Log("ERROR: " & j.ErrorMessage)
        End If
        j.Release
End Sub

Sub BntEnd_Click
Activity.Finish   
End Sub

Sub BtnTestRead_Click
    GetRecord
End Sub

Sub BtnTestCreate_Click
    CreateTable
End Sub


Sub BtnTestInsert_Click
    InsertRecord("fox")
End Sub

Sub BtnTestReadOne_Click
    GetOneRecord(1)
End Sub

GetOneRecord or GetRecord don't retrieve values.
Log on java server tells me it received command:
B4X:
Command: query: select_one_animal, took: 1ms, client=192.168.58.91
or:
B4X:
Command: query: select_animals, took: 1ms, client=192.168.58.91
But if i put a breakpoint in b4a on log line, watching 'res' it is empty
If I uncomment req.printtable I get error
B4X:
Error occurred on line: 123 (DBRequestManager)
java.lang.NullPointerException

Can anyone help?
When I use old rdc everything works well.
Thanks
 

BillMeyer

Well-Known Member
Licensed User
Longtime User
Try this first:

B4X:
   Log("DB Result: "&res)
   Log(req.PrintTable(res))

If you get a result there then you can use something similar to this to get the result and work with it.

B4X:
   If j.Success Then
        req.HandleJobAsync(j, "req")
    
        Wait For (req) req_Result(res As DBResult)
    
        Log("DB Result: "&res)
        Log(req.PrintTable(res))
    
        For Each row() As Object In res.Rows
            Log("Serial: "&row(res.Columns.Get("Serial"))) ' "Serial" is a column name in your table
            Log("IMSI Number: "&row(res.Columns.Get("IMSINumber")))
            Log("Cell Number: "&row(res.Columns.Get("CellNumber")))
            Log("Pin Number: "&row(res.Columns.Get("Pin")))
            Log("Active: "&row(res.Columns.Get("Active")))

            If row(res.Columns.Get("Active")) = "Y" Then
                CActive = "Y"
            Else
                CActive = "N"
            End If
        Next
    
    Else
        Log("ERROR: " & j.ErrorMessage)
    End If
    j.Release

I trust that this will assist.
 
Upvote 0

Milan Bozic

Member
Licensed User
Unfortunately the result is empty, that's just the problem
Here the log as you kindly suggested:
B4X:
DB Result: [rows=null, Tag=null, columns=null
, IsInitialized=true]
done one record
Error occurred on line: 123 (DBRequestManager)
java.lang.NullPointerException: expected receiver of type anywheresoftware.b4a.objects.collections.Map, but got null
    at java.lang.reflect.Method.invokeNative(Native Method)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Error occurred on line: 123 (DBRequestManager)
java.lang.NullPointerException
In your DBRequestManger module, what is line 123 (and before and after)?
 
Upvote 0

Milan Bozic

Member
Licensed User
And further on: in line 92 of b4j server I can watch contents of data(); in this case data[0] to data[128]
and in HandleJobAsync of DBRequestManager in b4a when line no.80 executes:

B4X:
Dim data() As Byte = Bit.InputStreamToBytes(Job.GetInputStream)

I read same values of data() in android
so obviously data is passed from b4j server to b4a client :)
but then something else does not work :confused:
 
Upvote 0

Milan Bozic

Member
Licensed User
for Oliver:
line 123 is the PrintTable routine, she errors out because Table is empty (null)
B4X:
java.lang.NullPointerException: Attempt to invoke virtual method 'int anywheresoftware.b4a.objects.collections.Map.getSize()' on a null object reference
that's just the point, table should not be empty
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Modify HandleJobAsync with some log information:
B4X:
Public Sub HandleJobAsync(Job As HttpJob, EventName As String)
   Log("HandleJobAsync: Job: " & Job) ' May show a warning in the IDE
   Dim ser As B4XSerializator
   Dim data() As Byte = Bit.InputStreamToBytes(Job.GetInputStream)
   Log("HandleJobAsync: data().Length: " & data.Length()")
   ser.ConvertBytesToObjectAsync(data, "ser")
   Wait For (ser) ser_BytesToObject (Success As Boolean, NewObject As Object)
   If Success = False Then
       Log("Error reading response: " & LastException)
       Return
   End If
   Log("HandleJobAsync: NewObject: " & NewObject) ' May show a warning in the IDE
   Dim res As DBResult = NewObject
   res.Tag = Job.Tag
   Log("HandleJobAsync: res: " & res) ' May show a warning in the IDE
   CallSubDelayed2(mTarget, EventName & "_result", res)
End Sub
 
Upvote 0

Milan Bozic

Member
Licensed User
ok, good idea

B4X:
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
HandleJobAsync: Job: [errormessage=, httputils2service=null, jobname=DBRequest
, main=null, password=, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@414106f0
, response=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpResponse@413ecc70, starter=null, username=
, tag=null, target=class b4a.example.main, taskid=1
, success=true]
HandleJobAsync: data().Length:
129
HandleJobAsync: NewObject: [rows=null, Tag=null, columns=null
, IsInitialized=true]
HandleJobAsync: res: [rows=null, Tag=null, columns=null
, IsInitialized=true]
DB Result: [rows=null, Tag=null, columns=null
, IsInitialized=true]
done one record
Error occurred on line: 144 (DBRequestManager)
java.lang.NullPointerException
    at b4a.example.dbrequestmanager._printtable(dbrequestmanager.java:229)
    at b4a.example.main$ResumableSub_GetOneRecord.resume(main.java:853)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
    at anywheresoftware.b4a.shell.Shell.runGoodChain(Shell.java:475)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:293)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:250)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
    at anywheresoftware.b4a.BA$2.run(BA.java:370)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)

so data length is 129 as expected, but then newobject is empty

seems one of these two lines is failing:

B4X:
ser.ConvertBytesToObjectAsync(data, "ser")
   Wait For (ser) ser_BytesToObject (Success As Boolean, NewObject As Object)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Type DBResult (Tag As Object,columns As Map, rows As List)
Type members are case sensitive. Try
B4X:
Type DBResult (Tag As Object, Columns As Map, Rows As List)
 
Upvote 0
Top