Android Question Error getting map key value

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
I created a map and I populated it like this:

B4X:
map.Put("2016062309343853.jpg", "c:\desktop")

When I want to retrieve the key value, i do this

B4X:
Dim i as int
i = 0
map.GetKeyAt(i)

but I get this error log:

B4X:
java.lang.NumberFormatException: Invalid double: "2016062309343853.jpg"
    at java.lang.StringToReal.invalidReal(StringToReal.java:63)
    at java.lang.StringToReal.parseDouble(StringToReal.java:269)
    at java.lang.Double.parseDouble(Double.java:295)
    at it.android.imgspa.and_FotoContainer.svcupload._smb1_listcompleted(svcupload.java:409)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA$3.run(BA.java:320)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:153)
    at android.app.ActivityThread.main(ActivityThread.java:5297)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)

I read that:
The key should be a string or a number. The value can be any type of object.

So what's wrong?
 

eurojam

Well-Known Member
Licensed User
Longtime User
I suppose your code is like this
B4X:
Dim i as int
i = 0
myVariable = map.GetKeyAt(i)
deppending on what Type is myvaraible - if it is a double type - you will get an error...?!
 
Upvote 0

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
I suppose your code is like this
B4X:
Dim i as int
i = 0
myVariable = map.GetKeyAt(i)
deppending on what Type is myvaraible - if it is a double type - you will get an error...?!

My code it's like this:

B4X:
Dim k As String
Dim i as int
i = 0
k = map.GetKeyAt(i)

I need a string, not a double
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Your file name used for the key is evaluated as being a number, a double due to the "decimal" point, so the conversion from string to doubke fails for what is the file extension but it is read as the decimal part of your double.
 
Upvote 0

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Your file name used for the key is evaluated as being a number, a double due to the "decimal" point, so the conversion from string to doubke fails for what is the file extension but it is read as the decimal part of your double.

I don't understand. How could be a double, if there is the word ".jpg"?
 
Upvote 0

udg

Expert
Licensed User
Longtime User
I guess the problem arises because the filename part is an all-digits one so something looks at it like a number and having a full stop between filename and extension that makes it for a decimal number so the conversion routine from string to double which obviously fails.
Try without the full stop and it will work..just for testing.
 
Upvote 0

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
I guess the problem arises because the filename part is an all-digits one so something looks at it like a number and having a full stop between filename and extension that makes it for a decimal number so the conversion routine from string to double which obviously fails.
Try without the full stop and it will work..just for testing.

Without the full stop? What do you mean?
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
I have tried this snipplet without any error....
B4X:
    Dim i As Int
    Dim map As Map
    map.Initialize
    map.Put("2016062309343853.jpg", "c:\desktop")
    i = 0
    Dim k As String = map.GetKeyAt(i)
    Log(k)
 
  • Like
Reactions: udg
Upvote 0

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
I have tried this snipplet without any error....
B4X:
    Dim i As Int
    Dim map As Map
    map.Initialize
    map.Put("2016062309343853.jpg", "c:\desktop")
    i = 0
    Dim k As String = map.GetKeyAt(i)
    Log(k)
Neither me. But in another part of code, yes
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your file name used for the key is evaluated as being a number, a double due to the "decimal" point, so the conversion from string to doubke fails for what is the file extension but it is read as the decimal part of your double.
This is not the reason for this error.

Please run your code in debug mode and post the line that raises the error.

Note that it is a mistake to use Map.GetKeyAt, but it is not related to this error.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Reading post #9 it seems that the problematic programs sportse two code blocks supposed to be identical, where only one causes the error. If this is the case I'll try to figure out the difference between the two since @eurojams code works properly and the OP confirms it.
 
Upvote 0

Similar Threads

Top