Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to avaoid java.lang.IllegalArgumentException: No enum const class

912973May 17 2012 — edited May 21 2012
HI ,

Iam getting java.lang.IllegalArgumentException when iam using switch case through Enum contants.

//Enum Constants declaration
public enum USEOFPROCEEDSVALUES { U1,U2,U3, U4}




//Using Enum in Java class
Test.java

USEOFPROCEEDSVALUES useOfProceedsVar =USEOFPROCEEDSVALUES.valueOf(useOfproceeds);

switch (useOfProceedsVar) {
case U1:

revenueSourceCode="REVENUE_SOURCE_CODE.POWER";

break;
case U2:
revenueSourceCode="REVENUE _SOURCE_CODE.WATER";
break;
case U3:
revenueSourceCode="REVENUE_SOURCE_CODE.POWER";
break;
case U4:
revenueSourceCode=REVENUE_SOURCE_CODE.POWER";
}
break;
default:
revenueSourceCode=null;



Exception raising if there is either of these not U1,U2,U3,U4 ara not avalabele. i.e is if useOfProceedsVar is A6 then exception raising

How to avoid this exception

Thanks for early reply

Comments

Tolls
First off, how can useOfProceedsVar be anything other than one of the 4 values in the enum or null?
Second, print the full exception and stack trace, highlighting the line on which it is thrown.
And please use code tags when posting code.
EJP
Exception raising if there is either of these not U1,U2,U3,U4 ara not avalable. i.e is if useOfProceedsVar is A6 then exception raising
Correct. That's what it's specified to do. See the Javadoc.
How to avoid this exception
Don't feed it illegal arguments.

Note: this is poorly designed code. You could make the strings you are assigning into member functions of the enum. Then you wouldn't need the switch statement.
452196
user818909 wrote:
HI ,

Iam getting java.lang.IllegalArgumentException when iam using switch case through Enum contants.

//Enum Constants declaration
public enum USEOFPROCEEDSVALUES { U1,U2,U3, U4}




//Using Enum in Java class

Exception raising if there is either of these not U1,U2,U3,U4 ara not avalabele. i.e is if useOfProceedsVar is A6 then exception raising
Actually useOfProceedsVar can never be A6, it can only take a value from the enum.

The exception will be raised by valueOf, which (quite correctly) throws it if the String you pass to it doesn't match any of the enum constants.

>
How to avoid this exception
Don't avoid it, process it. What do you want your code to do if the string doesn't match any of your enum constants? Whatever it is, stick it in a catch clause.
EJP
Actually useOfProceedsVar can never be A6, it can only take a value from the enum.
I think he means 'useOfproceeds', which by the signature of valueOf() is clearly a String.
934982
Why not directly use String ? Switch it ?


Ksharp
EJP
Only because he already has the enum, and by a simple extension can get a much cleaner implementation ...
934982
Maybe OP can use a simple condition judge statement to avoid these error message.
  public static void main(String[] args) {
        String useOfproceeds="A1";
    if (useOfproceeds.equals("U1") ||
       useOfproceeds.equals("U2") ||
       useOfproceeds.equals("U3") ||
       useOfproceeds.equals("U4")) {
    USEOFPROCEEDSVALUES useOfProceedsVar =USEOFPROCEEDSVALUES.valueOf(useOfproceeds);
    Test test=new Test();
    test.tellItLikeItIs( useOfProceedsVar );
    System.out.println(test.revenueSourceCode);}
Ksharp
EJP
A complete waste of time and space. valueOf() has to validate the value anyway. Let it do that, and let it throw the exception, and catch it, and do whatever you have to do about the invalid value in the catch block. Testing things twice doesn't accomplish anything.
1 - 8
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jun 18 2012
Added on May 17 2012
8 comments
14,446 views