Android Question Same code in B4J doesn't work in B4A....

LWGShane

Well-Known Member
Licensed User
Longtime User
Testing my AstralCore library on Android.

B4J:
B4X:
    Dim TestCore As LocalCore
    TestCore.Initialize(SpecialFolder.Documents, "actest", "test")
   
    Dim Months As List
    Months.Initialize
    Months.AddAll(Array As String("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"))

    For i = 0 To Months.Size - 1
        TestCore.Put("Months", i, Months.Get(i))
    Next
   
    For Each Key As String In TestCore.Keys("Months")
        Log ($"[Key: ${Key}][Month: ${TestCore.Get("Months", Key)}]"$)
    Next

B4A; Just replace the initialize line with this:
B4X:
TestCore.Initialize(File.DirInternal, "actest", "test")



The code works fine in B4J but in B4A it throws the following error:
java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]

Why is this?
 

OliverA

Expert
Licensed User
Longtime User
Which line in your code is throwing this exception?
 
Upvote 0

LWGShane

Well-Known Member
Licensed User
Longtime User
Which line in your code is throwing this exception?

The Log() statement in the For Each statement.
B4X:
For Each Key As String In TestCore.Keys("Months")
    Log ($"[Key: ${Key}][Month: ${TestCore.Get("Months", Key)}]"$)
Next
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Are you sure it should not be
B4X:
For Each Key As Int In TestCore.Keys("Months")
    Log ($"[Key: ${Key}][Month: ${TestCore.Get("Months", Key)}]"$)
Next

sine you use an Int as key (the variable i) when creating the entry?
 
Upvote 0

LWGShane

Well-Known Member
Licensed User
Longtime User
Are you sure it should not be
B4X:
For Each Key As Int In TestCore.Keys("Months")
    Log ($"[Key: ${Key}][Month: ${TestCore.Get("Months", Key)}]"$)
Next

sine you use an Int as key (the variable i) when creating the entry?
I tried that and the Log() statement is still throwing the error. Plus, why would "As String" work in B4J and not B4A?
 
Upvote 0

Emme Developer

Well-Known Member
Licensed User
Longtime User
I tried that and the Log() statement is still throwing the error. Plus, why would "As String" work in B4J and not B4A?
Map Keys hold object value. So you need to match type when you get a value from map using the key.

B4X:
Dim m As Map
    m.Initialize
    Dim Months As List
    Months.Initialize
    Months.AddAll(Array As String("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"))
   
    For i = 0 To Months.Size - 1
        m.Put(i, Months.Get(i))
    Next
  
    For Each Key As String In m.Keys
        Log ($"[Key: ${Key}][Month: ${m.Get(Key)}]"$)
    Next
   
    'Log ***
    [Key: 0][Month: null]
    [Key: 1][Month: null]
    [Key: 2][Month: null]
    [Key: 3][Month: null]
    [Key: 4][Month: null]
    [Key: 5][Month: null]
    [Key: 6][Month: null]
    [Key: 7][Month: null]
    [Key: 8][Month: null]
    [Key: 9][Month: null]
    [Key: 10][Month: null]
    [Key: 11][Month: null]
   
    For Each k As Int In m.Keys
        Log ($"[Key: ${Key}][Month: ${m.Get(k)}]"$)
    Next
   
    'Log ***
   
    [Key: 11][Month: January]
    [Key: 11][Month: February]
    [Key: 11][Month: March]
    [Key: 11][Month: April]
    [Key: 11][Month: May]
    [Key: 11][Month: June]
    [Key: 11][Month: July]
    [Key: 11][Month: August]
    [Key: 11][Month: September]
    [Key: 11][Month: October]
    [Key: 11][Month: November]
    [Key: 11][Month: December]

By the way you are using your library, not a simple maps, so i don't know why get the error without using the library
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
See what the type is that is returned from your call to TestCore.Get

B4X:
For Each Key As String In TestCore.Keys("Months")
    Log(GetType(TestCore.Get("Months", Key))) ' See what type is returned
    Log(TestCore.Get("Months", Key)) ' See what's coming back
    Log ($"[Key: ${Key}][Month: ${TestCore.Get("Months", Key)}]"$)
Next

This is to diagnose what your library returns. You could run this on B4J and B4A and see if there is a difference. What is your backend database for B4J? B4A? How are you defining the tables in either version?
 
Upvote 0

LWGShane

Well-Known Member
Licensed User
Longtime User
See what the type is that is returned from your call to TestCore.Get

B4X:
For Each Key As String In TestCore.Keys("Months")
    Log(GetType(TestCore.Get("Months", Key))) ' See what type is returned
    Log(TestCore.Get("Months", Key)) ' See what's coming back
    Log ($"[Key: ${Key}][Month: ${TestCore.Get("Months", Key)}]"$)
Next

B4A: All three return the same error. ("Object cannot be converted to String")

B4J: All three work the way they should. (See below.)
Waiting for debugger to connect...
Program started.
java.lang.String
January
[Key: 0][Month: January]
java.lang.String
February
[Key: 1][Month: February]
java.lang.String
November
[Key: 10][Month: November]
java.lang.String
December
[Key: 11][Month: December]
java.lang.String
March
[Key: 2][Month: March]
java.lang.String
April
[Key: 3][Month: April]
java.lang.String
May
[Key: 4][Month: May]
java.lang.String
June
[Key: 5][Month: June]
java.lang.String
July
[Key: 6][Month: July]
java.lang.String
August
[Key: 7][Month: August]
java.lang.String
September
[Key: 8][Month: September]
java.lang.String
October
[Key: 9][Month: October]

I just tried opening the DB that was created with B4A with B4J and B4J could open it. (It displays the above.)


What is your backend database for B4J? B4A?
Local instances for B4A and B4J are SQLite.

How are you defining the tables in either version?
B4X:
CREATE TABLE IF NOT EXISTS Main (TableName TEXT, KeyName TEXT, ValueObj BLOB, PRIMARY KEY(TableName, KeyName))
There's no difference because I use the same code modules to compile the libraries.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
You need to go to your library and back trace your code. Log the type and value of your return inside your get method. Something in b4a or your code does not provide a ToString method.
 
Upvote 0

LWGShane

Well-Known Member
Licensed User
Longtime User
Oh crap, lookie what I found here:
Dim RS As ResultSet = DB.ExecQuery2("SELECT Value FROM Main WHERE TableName = ? AND Key = ?", Array As Object(FixEmptyTable(TableName), Key))

Corrected it to String and I can successfully read the database.

Thanks to all who tried solving this! @OliverA - Thanks again for the help!
 
Upvote 0
Top