What is the reason for java.lang.IllegalArgumentException: No enum const class even though iterating through values() works just fine?

前端 未结 4 1234
执念已碎
执念已碎 2020-12-05 12:55

This question is basically an extension of my previous question . I asked the previous question to be sure that the Enum constants are populated when the class loads . Here\

相关标签:
4条回答
  • 2020-12-05 13:15

    I had parsing enum problem when i was trying to pass Nullable Enum that we get from Backend. Of course it was working when we get value, but it was problem when the null comes up.

    java.lang.IllegalArgumentException: No enum constant

    Also the problem was when we at Parcelize read moment write some short if.

    My solution for this was

    1.Create companion object with parsing method.

    enum class CarsType {
        @Json(name = "SMALL")
        SMALL,
        @Json(name = "BIG")
        BIG;
    
        companion object {
            fun nullableValueOf(name: String?) = when (name) {
                null -> null
                else -> valueOf(name)
            }
        }
    }
    

    2. In Parcerable read place use it like this

    data class CarData(
        val carId: String? = null,
        val carType: CarsType?,
        val data: String?
    ) : Parcelable {
        constructor(parcel: Parcel) : this(
            parcel.readString(),
            CarsType.nullableValueOf(parcel.readString()),
            parcel.readString())
    
    0 讨论(0)
  • 2020-12-05 13:16

    That's because you defined your own version of name for your enum, and getByName doesn't use that.

    getByName("COLUMN_HEADINGS") would probably work.

    0 讨论(0)
  • 2020-12-05 13:26

    Instead of defining: COLUMN_HEADINGS("columnHeadings")

    Try defining it as: COLUMNHEADINGS("columnHeadings")

    Then when you call getByName(String name) method, call it with the upper-cased String like this: getByName(myStringVariable.toUpperCase())

    I had the same problem as you, and this worked for me.

    0 讨论(0)
  • 2020-12-05 13:29

    Enum.valueOf() only checks the constant name, so you need to pass it "COLUMN_HEADINGS" instead of "columnHeadings". Your name property has nothing to do with Enum internals.


    To address the questions/concerns in the comments:

    The enum's "builtin" (implicitly declared) valueOf(String name) method will look up an enum constant with that exact name. If your input is "columnHeadings", you have (at least) three choices:

    1. Forget about the naming conventions for a bit and just name your constants as it makes most sense: enum PropName { contents, columnHeadings, ...}. This is obviously the most convenient.
    2. Convert your camelCase input into UPPER_SNAKE_CASE before calling valueOf, if you're really fond of naming conventions.
    3. Implement your own lookup method instead of the builtin valueOf to find the corresponding constant for an input. This makes most sense if there are multiple possible mappings for the same set of constants.
    0 讨论(0)
提交回复
热议问题