B4J Question Map and LinkedHashMap

rdkartono

Member
Licensed User
Longtime User
Supposed I have these code :
B4J code:
Private Sub sioserver_set_audio_configuration(sock As NettySocketWrapper, value As Object, ack As AckRequest)
    Log($"Event set_audio_configuration"$)
    If (ack <> Null) Then
        If (ack.isAckRequested) Then Log("Acknowledge required") Else Log("Acknowledge not required")
    Else
        Log("ack is null")
    End If
    If (value<>Null) Then
        Dim valtype As String = GetType(value)
        Log($"value type = ${valtype}"$)
      
        If (valtype.Contains("Map")) Then
            Dim valuemap As Map = value.As(Map)
            If (valuemap.IsInitialized) Then
                If (valuemap.Size>0) Then
                    Log($"valuemap size = ${valuemap.Size}"$)
                  
                    For Each kk As String In valuemap.Keys
                        Log($"Key = ${kk}, Value = ${valuemap.Get(kk)}"$)
                    Next
                Else
                    Log("Valuemap size = 0")
                End If
            Else
                Log("Valuemap not initialized")             
            End If
        Else if (value Is String) Then
            Log("value is string")
        Else
            Log("value is something else")
        End If
      
    Else
        Log($"set_audio_configuration value is null"$)
    End If
End Sub

The code will crash at Map "For Each kk as string" line :
crash log:
Will raise sioserver_set_audio_configuration
jSocketIOServer StartListen at Port = 9000
Client Connected, ID = 6c598310-ea7a-43e2-b709-011ffdfba85d
Event get_network_configuration
get_network_configuration value is null
Event get_audio_configuration
get_audio_configuration value is null
valuetype = java.util.LinkedHashMap
value = {volume_input=0, volume_output=0, switchip=false}
Event set_audio_configuration
Acknowledge required
value type = java.util.LinkedHashMap
valuemap size = 3
Error occurred on line: 128 (Main)
java.lang.RuntimeException: method not supported. Use For Each instead.
    at anywheresoftware.b4a.objects.collections.Map.GetKeyAt(Map.java:114)
    at anywheresoftware.b4a.objects.collections.Map$IterableMap.Get(Map.java:184)
    at b4j.example.main._sioserver_set_audio_configuration(main.java:340)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

Even if I modified the code become this :
modified For using index:
For ii = 0 To valuemap.Size-1
                        Log($"Key = ${valuemap.GetKeyAt(ii)}"$)
                        Log($"Value = ${valuemap.GetValueAt(ii)}"$)
                    Next

Will still crash the same as above

I use B4J 9.80 (64 bit)


Any help ?
 
Last edited:

rdkartono

Member
Licensed User
Longtime User
Apparently my Library is using previous jCore.jar (pre-64 bit version), and I am using B4J 9.80 (64 bit version).
If I change Referenced Libraries to jCore 9.80, there is Type Mismatch :

Type mismatch: cannot convert from Map to Map.MyMap

Type Mismatch:
public boolean InitializeFromMap(Map value) {
        if (value instanceof Map) {
            if (value.IsInitialized()) {
                MyMap mm = value.getObject();
                .....
            }
        }
        return false;
    }
 
Upvote 0

rdkartono

Member
Licensed User
Longtime User
If I ran using B4J 9.30, the latest 32bit version, another crash showed
crash log:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap

At this point, I am hesitate to continue my project using B4J 9.80 (64bit) , since I need to maintain previous projects using 32 bit B4J 9.30.
That might cause lots of problem in previous projects.
 
Upvote 0

rdkartono

Member
Licensed User
Longtime User
Finally work correctly, using jCore 9.30 and B4J 9.30.

However I need advice if if need to do something with the Map, so can compatible both 9.30 and 9.80
 
Upvote 0

rdkartono

Member
Licensed User
Longtime User
The B4X code is compatible. It will work as long as you don't compile the code as a jar library. Create a b4xlib instead.
Unfortunately my library is an accumulated big and complicated libraries. Already create (jar + xml) since long time. To convert them become b4xlib, might need a lot of work to do ...

if no other suggestion , then I think still prefer to stay on B4J 9.30.
 
Upvote 0
Top