Android Question [Solved]Cannot cast to string?

RMarra

Member
Licensed User
The code below works perfectly if I single step with the debugger but give me this error if I remove the break point:

java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]
B4X:
    Dim BoatData(3) As String
    BoatData = BoatList.Get(Position)
 

Jeffrey Cameron

Well-Known Member
Licensed User
Try:
B4X:
Boatdata(0) = BoatList.get(Position)
Perhaps your list doesn't contain an array of strings, but a single string.
 

RMarra

Member
Licensed User
That didn't work. I also tried
B4X:
 Dim BoatData(3) As String = Array As String("","","")
and that seemed to fix it for a few minutes. I'm getting the same error at everyplace I "get" from a list. I try to figure out what's wrong and it starts working again for a little while and then the cycle starts over.
 

sorex

Expert
Licensed User
is Boatlist really containing a string? or is it a type or something else?

do a log(Boatlist) and show us the outcome.
 

sorex

Expert
Licensed User
if it are strings you would see the text there or java.lang.String if it is a reference to a string object and not java.lang.Object.

so something got messed up but we need to see more code to figure that out.
 

RMarra

Member
Licensed User
I deleted all the data and entered new data and the same thing happens. I put a try/catch around it and I get the data, but it breaks on another list the same way. As long as I have a breakpoint set, I can single step through it and it works fine. I'm going to tear the code out and rewrite it.
 

RMarra

Member
Licensed User
I put a try/catch around EVERY list.get() on EVERY list and now it still logs errors all over the place. As soon as I add a break point ANYWHERE in the code, everything works and the data is there. I don't even recompile! just add a breakpoint to already running code and all is well. Remove it and it breaks.

My project has four activities and only one module is having this trouble. I'm really concerned. The next thing I need to do is a list of b4xtables. I really don't believe this is going to work.
 
Last edited:

OliverA

Expert
Licensed User
What creates/how is boatlist created?
 

emexes

Well-Known Member
Licensed User
It looks like (i) BoatList.get() is returning an array of objects, not strings, and (ii) casting does not work for whole arrays (cf individual elements). Perhaps give this a spin:
B4X:
Dim BoatData() As Object =  BoatList.Get(Position)
For I = 0 to BoatData.Length - 1
    Log(I & " = [" & BoatData(I) & "]")    'cast from (String) Object to String should work ok
Next
 

RMarra

Member
Licensed User
using the array of object, as you suggested, seems to have fixed it. I'll do some more testing and let you know. Thank you very much.
 

emexes

Well-Known Member
Licensed User
using the array of object, as you suggested, seems to have fixed it. I'll do some more testing and let you know. Thank you very much.
Bear in mind that, whilst the array element is an object that is a string, it could potentially be any other type of object too. At compile time, the compiler does not know what type the array element object is, and if you want to use a string method on it, then you will need to cast it to a String first, eg:
B4X:
'BoatList.Get(1)    'returns the second array of objects (index 1) from the list of boats
'BoadList.Get(1)(2)    'returns third object (index 2) from the array of objects
'BoatList.Get(1)(2).ToUpperCase.Contains("DIESEL")    'won't compile because the compiler does not know what type of Object it is and thus what methods are available

Dim S2 As String = BoatList.Get(1)(2)    'B4A/Java can/will convert Object to String (especially if Object is a String... but it could be an Int, Float, Boolean etc too)
Log( S2.ToUpperCase.Contains("DIESEL") )    'no worries - compiler knows what methods are available for a String
Some languages can leave the what-methods-does-this-object-have question* to be resolved when the program runs, but there is a performance hit, especially if the type of the object can change from loop to loop.


*closely followed by the and-where-the-heck-are-they question ;-)
 
Top