Reputation: 53873
In my Android app I've got this method which takes a UUID. Unfortunately when I do this:
OverviewEvent overviewevent = eventAdapter.getOverviewEvent(UUID.fromString("0f14d0ab-9605-4a62-a9e4-5ed26688389b"));
I get an error saying java.lang.IllegalArgumentException: Invalid UUID: 100
The implementation of the getOverviewEvent is as follows:
public OverviewEvent getOverviewEvent(UUID uuid) throws Exception {
// Do stuff
}
Does anybody know how I can solve this?
Upvotes: 13
Views: 66247
Reputation: 2272
Did you copy and paste the code, I have found that a few characters that look correct are in fact the wrong ACSII code.
Remove the - and replace them again.
I have had this often with " as different editors/computers may use a slightly different code.
Upvotes: 1
Reputation: 930
I had this problem too. I think this is because my Java version is low. I found that in my Android application, uuid.split("-")
and uuid.replace("-")
are both useless. I guess it may be caused by that Java regards "-" as a regular expression. However when I try uuid.split("\\-")
and uuid.replace("\\-")
, they didn't work either. I don't know why this happened. I think it's a bug of Java.
According to Fildor's comment, in Android's implementation, uuid.split("-")
is used to split the uuid string into 5 parts. Then because of the mentioned bug, the uuid string couldn't be spliced into 5 parts. So "Invalid UUID" exception is thrown.
However, we can modify android's source code to avoid this problem. Using substring()
method, we can split the uuid string into 5 parts. And then we can make the uuid.
The following codes worked for me:
public static UUID makeUuid(String uuidString) {
String[] parts = {
uuidString.substring(0, 7),
uuidString.substring(9, 12),
uuidString.substring(14, 17),
uuidString.substring(19, 22),
uuidString.substring(24, 35)
};
long m1 = Long.parseLong(parts[0], 16);
long m2 = Long.parseLong(parts[1], 16);
long m3 = Long.parseLong(parts[2], 16);
long lsb1 = Long.parseLong(parts[3], 16);
long lsb2 = Long.parseLong(parts[4], 16);
long msb = (m1 << 32) | (m2 << 16) | m3;
long lsb = (lsb1 << 48) | lsb2;
return new UUID(msb, lsb);
}
Upvotes: 0
Reputation: 533500
Here is a workaround which avoids using this method,
String s = "0f14d0ab-9605-4a62-a9e4-5ed26688389b";
String s2 = s.replace("-", "");
UUID uuid = new UUID(
new BigInteger(s2.substring(0, 16), 16).longValue(),
new BigInteger(s2.substring(16), 16).longValue());
System.out.println(uuid);
prints
0f14d0ab-9605-4a62-a9e4-5ed26688389b
Upvotes: 19