Bug? Returning a List of Lists from Java Library throws error

tchart

Well-Known Member
Licensed User
Longtime User
I have a simple library that returns a B4X List of Lists, here is a snippet of the library.

Java:
package nz.ope.test;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.keywords.Common;
import anywheresoftware.b4a.objects.collections.Map;
import anywheresoftware.b4a.objects.collections.List;

@ShortName("ListList")

public class ListList {

    boolean IsInitialised = false;

    public void Initialize() {
        try {       
            IsInitialised = true;
        } catch (Exception e) {
            Common.Log(e.toString());
            IsInitialised = false;
        }
    }

    public List Test() {
        List parent = new List();
        parent.Initialize();
    
        List child1 = new List();
        child1.Initialize();
    
        child1.Add("A");
        child1.Add("B");
        child1.Add("C");
    
        List child2 = new List();
        child2.Initialize();
    
        child2.Add("A");
        child2.Add("B");
        child2.Add("C");
    
        parent.Add(child1);
        parent.Add(child2);
    
        return parent;
    }
}

When executed from B4J like this it throws an error;

B4X:
Sub AppStart (Args() As String)
    Dim L As ListList
    L.Initialize
 
    Dim parent As List = L.Test
 
    Dim child1 As List = parent.Get(0)
    Dim child2 As List = parent.Get(1)
 
    Log("Hello world!!!")
End Sub

The error is;

java.lang.ClassCastException: class anywheresoftware.b4a.objects.collections.List cannot be cast to class java.util.List (anywheresoftware.b4a.objects.collections.List is in unnamed module of loader 'app'; java.util.List is in module java.base of loader 'bootstrap')

Looking at the Java src for the B4J project it seems to be assuming the child object is a Java List rather than a B4X List.

Java:
_child1 = new anywheresoftware.b4a.objects.collections.List();
_child1 = (anywheresoftware.b4a.objects.collections.List) anywheresoftware.b4a.AbsObjectWrapper.ConvertToWrapper(new anywheresoftware.b4a.objects.collections.List(), (java.util.List)(_parent.Get((int) (0))));

Not sure whether this is a bug or intended behaviour?

BTW I have worked around this for now by using "ArrayList<ArrayList<String>" instead of List which works fine in B4J.
 
Last edited:

drgottjr

Expert
Licensed User
Longtime User
also working fine in b4j is coding in b4j. (image 1)

also working fine in java if you use java classes. (image 2)
B4X:
import java.util.List;
import java.util.ArrayList;

    public static List Test() {
        List parent = new ArrayList();
    
        List child1 = new ArrayList();
    
        child1.add("A");
        child1.add("B");
        child1.add("C");
    
        List child2 = new ArrayList();
    
        child2.add("A");
        child2.add("B");
        child2.add("C");
    
        parent.add(child1);
        parent.add(child2);
    
        return parent;
    }
 

Attachments

  • list.png
    list.png
    17.5 KB · Views: 84
  • list2.png
    list2.png
    31.2 KB · Views: 87
Last edited:

tchart

Well-Known Member
Licensed User
Longtime User
also working fine in b4j is coding in b4j. (image 1)

also working fine in java if you use java classes. (image 2)
B4X:
import java.util.List;
import java.util.ArrayList;

    public static List Test() {
        List parent = new ArrayList();
   
        List child1 = new ArrayList();
   
        child1.add("A");
        child1.add("B");
        child1.add("C");
   
        List child2 = new ArrayList();
   
        child2.add("A");
        child2.add("B");
        child2.add("C");
   
        parent.add(child1);
        parent.add(child2);
   
        return parent;
    }
It does indeed work with ArrayList I’m already using that work around. The problem is when returning B4X List of Lists in Java.
 
Top