B4J Question Return a b4x map object from a java inline sub

andrewmp

Member
Licensed User
Longtime User
I have tried this code but it is not working , I get "java.lang.ClassCastException: class anywheresoftware.b4a.objects.collections.Map cannot be cast to class anywheresoftware.b4a.objects.collections.Map$MyMap (anywheresoftware.b4a.objects.collections.Map and anywheresoftware.b4a.objects.collections.Map$MyMap are in unnamed module of loader 'app')", does anyone know where the problem lies ?

test:
Sub Button1_Click
    Dim mp As Map=GetJavaMap
    Dim s As Int=mp.Size
    xui.MsgboxAsync("Size", s)
End Sub

Public Sub GetJavaMap() As Map
    
    Dim m As Map
    m.Initialize
    Dim mo As JavaObject = Me
    m=mo.RunMethod("getMyMap", Null)
    Return m
      
End Sub

#if java

import java.lang.reflect.*;
import java.io.Console;
import anywheresoftware.b4a.*;
import anywheresoftware.b4a.objects.collections.Map;
 
public static Map getMyMap()
{
    Map mpPrt = new Map();
    mpPrt.Initialize();
            
    Map mp = new Map();
    mp.Initialize();
    mp.Put(1,"Test 1");
    mp.Put(2,"Test 2");
    
    mpPrt.Put(1,mp);

    mp.Initialize();
    mp.Put(3,"Test 3");
    mp.Put(4,"Test 4");
    
    mpPrt.Put(2,mp);

    return mpPrt;
}

#End If

Is there another way to return a b4x map from java ?
 

andrewmp

Member
Licensed User
Longtime User
Thanks for your reply and help, I modified the code to:

B4X:
public static Map getMyMap()
{
    Map mpPrt = new Map();
    mpPrt.Initialize();
            
    Map mp = new Map();
    mp.Initialize();
    mp.Put(1,"Test 1");
    mp.Put(2,"Test 2");
    
    mpPrt.Put(1,mp.getObject());

    mp.Initialize();
    mp.Put(1,"Test 1");
    mp.Put(2,"Test 1");
    
    mpPrt.Put(2,mp.getObject());

    return mpPrt.getObject() ;
}

But now I get an error compiling: Compiling generated Java code. Error src\b4j\example\main.java:155: error: incompatible types: MyMap cannot be converted to Map return mpPrt.getObject() ; 1 error javac 14.0.1

I've attached the code as well
 

Attachments

  • testjava.zip
    19.5 KB · Views: 141
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Try
B4X:
Sub Button1_Click
    Dim mp As Map
    mp.Initialize

    Me.as(JavaObject).RunMethod("getMyMap", Array As Map(mp))
    Dim s As Int=mp.Size
    xui.MsgboxAsync("Size :" & s, "Map Size")
End Sub


#if java

import java.lang.reflect.*;
import java.io.Console;
import anywheresoftware.b4a.*;
import anywheresoftware.b4a.objects.collections.Map;

 
public static void getMyMap(Map mpPrt)
{
           
    Map mp = new Map();
    mp.Initialize();
    mp.Put(1,"Test 1");
    mp.Put(2,"Test 2");
    
    mpPrt.Put(1,mp);

    mp.Clear();
    mp.Put(3,"Test 3");
    mp.Put(4,"Test 4");
    
    mpPrt.Put(2,mp);
    System.out.println(mpPrt);
}

#End If
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…