Android Question Strange error

Lello1964

Well-Known Member
Licensed User
Longtime User
I have this code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
  
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private numTagliBanconote As Int = 6
    Private TagliBanconote()  As Int
    Private BanconoteDisponibili(10,3) As Int
  
    Private d1 As EditText
    Private d2 As EditText
    Private d3 As EditText
    Private d4 As EditText
    Private d5 As EditText
    Private d6 As EditText
    Private n1 As EditText
    Private n2 As EditText
    Private n3 As EditText
    Private n4 As EditText
    Private n5 As EditText
    Private n6 As EditText
  
    d1.Initialize("")
    d2.Initialize("")
    d3.Initialize("")
    d4.Initialize("")
    d5.Initialize("")
    d6.Initialize("")
    n1.Initialize("")
    n2.Initialize("")
    n3.Initialize("")
    n4.Initialize("")
    n5.Initialize("")
    n6.Initialize("")
  

  End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    d1.text ="20"
    d2.text ="5"
    d3.text ="2"
    d4.text ="0"
    d5.text ="0"
    d6.text ="0"
  
    n1.text ="5"
    n2.text ="10"
    n3.text ="20"
    n4.text ="10"
    n5.text ="50"
    n6.text ="100"

    btnAggiornaBanconote_Click
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnAggiornaBanconote_Click
    TagliBanconote = Array As Int(n1.text, n2.text, n3.text, n4.text, n5.text, n6.text)
    BanconoteDisponibili = Array (Array As Int(n6.text, d6.text,0,0), Array As Int(n5.text, d5.text,0,0), Array As Int(n4.text, d4.text,0,0), Array As Int(n3.text,d3.text,0,0), Array As Int(n2.text, d2.text,0,0), Array As Int(n1.text, d1.text,0,1))
End Sub


if i try to compile have this error :

*** Service (starter) Create ***
** Service (starter) Start **
Error occurred on line: 86 (Main)
java.lang.ClassCastException: java.lang.Object[] cannot be cast to int[][]
at b4a.example.main._btnaggiornabanconote_click(main.java:428)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at b4a.example.main.initializeGlobals(main.java:320)
at b4a.example.main.afterFirstLayout(main.java:101)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:82)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
** Activity (main) Create, isFirst = true **


but if i change this sub adding a blank line it work :

B4X:
Sub btnAggiornaBanconote_Click
    TagliBanconote = Array As Int(n1.text, n2.text, n3.text, n4.text, n5.text, n6.text)
  
    BanconoteDisponibili = Array (Array As Int(n6.text, d6.text,0,0), Array As Int(n5.text, d5.text,0,0), Array As Int(n4.text, d4.text,0,0), Array As Int(n3.text,d3.text,0,0), Array As Int(n2.text, d2.text,0,0), Array As Int(n1.text, d1.text,0,1))
End Sub

I can't explain it

??????
 
Last edited:

William Lancee

Well-Known Member
Licensed User
Longtime User
Everything after the last Private statement should be moved to Activity_Create. The Globals sub is only intended for variable declaration, and mostly for GUI related elements.
I think you really confused the compiler o_O
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
Everything after the last Private statement should be moved to Activity_Create. The Globals sub is only intended for variable declaration, and mostly for GUI related elements.
I think you really confused the compiler o_O
Move into Activity_Create, some problem.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
You cannot fill a multi-dimensional array the way you are doing it. BanconoteDisponibili is defined as a two dimensional array of Int. Yet when you say
B4X:
    BanconoteDisponibili = Array (...)
That is actually saying
B4X:
    BanconoteDisponibili = Array As Object (...)
So now you are assigning an object array to a variable that you declared as an int array. Therefore
java.lang.ClassCastException: java.lang.Object[] cannot be cast to int[][]
I also noticed that the "inner" arrays declare 4 elements
B4X:
Array As Int(n6.text, d6.text,0,0)
Yet, the second dimension of BanconoteDisponibili is only 3 elements
B4X:
Private BanconoteDisponibili(10,3) As Int
If you want to fill BanconoteDisponibili you would have to do something like
B4X:
Dim tempArray() as Int = Array As Int ( _
n6.text, d6.text,0, _
n5.text, d5.text,0, _
n4.text, d4.text,0, _
n3.text, d3.text,0, _
n2.text, d2.text,0, _
n1.text, d1.text,0)
For x = 0 to 5
  For y = 0 to 2
    BanconoteDisponibili(x,y) = tempArray(x*3 +y)
  Next
Next
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
It filled, but when you would use it, it would fail. Try it.
 
Upvote 0
Top