13

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?

kramer65
  • 50,427
  • 120
  • 308
  • 488
  • 1
    It runs fine on Java 7, so I suspect a bug. – Peter Lawrey Sep 18 '13 at 12:20
  • @PeterLawrey - Ah, yes, but a bug in what? Do you mean in the Android basics, or in my program? – kramer65 Sep 18 '13 at 12:22
  • In the `UUID.fromString()`, the one throwing the exception, it shouldn't be throwing. – Peter Lawrey Sep 18 '13 at 12:25
  • Android's implementation can be found here: https://android.googlesource.com/platform/libcore/+/refs/heads/master/luni/src/main/java/java/util/UUID.java – Fildor Sep 18 '13 at 12:41
  • As far as I can see there, it shouldn't print "Invalid UUID: **100**" ... Are you sure about the input string? Is it hard coded like in your question? – Fildor Sep 18 '13 at 12:46
  • This does not work for Java 8. See here bugs.java.com/view_bug.do?bug_id=8159339 . As an alternative you can use "UUID.fromString(uuid).toString().equals(uuid)" – ampofila Aug 08 '18 at 12:05

3 Answers3

19

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
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

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.

Recycled Steel
  • 2,272
  • 3
  • 30
  • 35
0

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);
}
Community
  • 1
  • 1
Cosmo
  • 836
  • 1
  • 12
  • 27