Skip to content
Advertisement

Compilation error due to a wrong creation of IndexOutOfBoundsException

I had written a code to show a billboard program and I wanted to prevent the system from crashing by using an IndexOutOfBoundsException when users enter an invalid choice. I can’t seem to get it to run properly.

Here is what I have:

if (index <= messages.size())
    this.text = messages.get(index - 1);
else
    throw new IndexOutOfBoundsException("Invalid Number Choice"); 

Here is my error:

Exception in thread “main” java.lang.RuntimeException: Uncompilable source code – constructor IndexOutOfBoundsException in class java.util.IndexOutOfBoundsException cannot be applied to given types;
 required: no arguments
 found: java.lang.String
 reason: actual and formal argument lists differ in length.

I really don’t know where to go from here. I tried try/catch and I can’t use the IndexOutOFBoundsException which I wanted to use because I have another Class I’m calling.

Advertisement

Answer

Good that you are using a StringIndexOutOfBoundsException.

However, I should mention that based on your error ” Uncompilable source code – constructor IndexOutOfBoundsException in class java.util.IndexOutOfBoundsException” You must have imported a different exception Class it should be java.lang.IndexOutOfBoundsException

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement