0

I have a ENUM defined as;

public enum YesNoEnum {
    Y,
    N
}

I am using this for one of my values voted.

So I use it within a loop (looping over object array from query response)

person.setVoted(responseArray[1] != null ? YesNoEnum.valueOf((String)responseArray[1]) : null);

I have the setter as

public void setVoted(YesNoEnum voted) {
    this.voted = voted;
}

Now I am getting an exception if the value for responseArray[1] is Y/N

If I debug/watch responseArray[1] , it shows type as YesNoEnum and shows value as "Y" It says

cannot cast an instance of YesNoEnum to an instance of String

7
  • This is String[] responseArray? Commented Nov 21, 2013 at 9:52
  • how is responseArray declared? Commented Nov 21, 2013 at 9:52
  • It is an Object array Object[] Commented Nov 21, 2013 at 9:54
  • So the code is while i am iterating using for(Object[] responseArray : response.getRecords()) Commented Nov 21, 2013 at 9:55
  • Never, ever, cast anything to String in Java, always use `toString()'. Casting is in general a bad idea in Java except when you have checked the type beforehand. Commented Nov 21, 2013 at 9:55

3 Answers 3

1

If I debug/watch responseArray[1] , it shows type as YesNoEnum and shows value as "Y" It says

If I understand right the responseArray is array of enum YesNoEnum.

Therefore you get error on casting enum instance to String:

(String)responseArray[1]

You can write just:

person.setVoted(responseArray[1]);

As a side note

to handle null I would change enum to:

public enum YesNoEnum {
    Y,
    N,
    UNKNOWN
}

and write:

person.setVoted(responseArray[1] != null ? responseArray[1] : YesNoEnum.UNKNOWN);
Sign up to request clarification or add additional context in comments.

1 Comment

If responseArray is of type Object[]as said in the question’s comments, a type cast is unavoidable. So it would be person.setVoted((YesNoEnum)responseArray[1]);
0

Use responseArray[1].toString() instead of (String)responseArray[1].

3 Comments

YesNoEnum.valueOf(responseArray[1].toString()) = responseArray[1] right?
Look at the answer above, IMHO it's a much better solution.
well, in this case you dont need to ask if its null because you anyways provide null to method
0

A YesNoEnum is not a String, it's an enum and you cannot cast from enum to String. You can either change the enum to a String by calling .toString(), or if you know that all objects in your array are of YesNoEnum types then you can skip the casting alltogether and do this:

person.setVoted(responseArray[1] != null ? responseArray[1] : null);

However, if the contents of responseArray are of a variety of types (some YesNoEnum, some String, some Object...), you will need to determine a way to transfrom each type into YesNoEnum objects, something better done in another method. As an aside, I highly recommend avoiding the use of mixed object types in your array. It creates all sorts of problems. Keep you arrays all of the same type. If you need to use mixed types, then take a differnt approach.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.