B4J Question List from array behavior

Emme Developer

Well-Known Member
Licensed User
Longtime User
Hi to all, i don't understand why this 3 operations have a different behaviors. Can someone explain why i got an exception, but by assigning the array at list in direct way all is ok

B4X:
Dim m As Map = CreateMap("name":"", "image":Array As String())

'First
    Dim l2 As List
    Dim o As Object = m.Get("image")
    l2 = o

    'Second
    Dim l1 As List = Array As String()

    'Third
    Dim l As List = m.Get("image")

When i try to execute this code first and third throw an exception

B4X:
Waiting for debugger to connect...
Program started.
Error occurred on line: 27 (Main)
java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.util.List
    at b4j.example.main._appstart(main.java:110)
    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:632)
    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)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at b4j.example.main.start(main.java:38)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
 

DonManfred

Expert
Licensed User
Longtime User
Please upload a small project showing the problem.
Error occurred on line: 27 (Main) java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.util.List
what happens in line 27?
You are trying to cast an array to a list
 
Upvote 0

Emme Developer

Well-Known Member
Licensed User
Longtime User
Please upload a small project showing the problem.

what happens in line 27?
You are trying to cast an array to a list

Line 27 is line 5 in code i shared. Also in line 9 i'm trying to cast an array in list, but it works properly. Simple project
 

Attachments

  • Nuova cartella.zip
    15.5 KB · Views: 144
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
No need for a project.
Arrays and lists are not the same thing.
The compiler automatically converts an array to a list. However this can only be done when the compiler knows, during compilation, that there is an array that should be converted to a list.

B4X:
Dim img() As Object = m.Get("image")
Dim l As List = img
 
Upvote 0
Top