Unhandled event loop execption - StringIndexOutOfBoundsException

This piece of code is causing a runtime error

String checkNamedRef = Arrays.toString((Object[]) namedRefComponent).substring(1, 5);

Open in new window


Error: Unhandled event loop execption
           java.lang.StringIndexOutOfBoundsException: String index out of range: 5

Here is the complete  Method

public static boolean hasDwgNamedReference(Object namedRefComponent){ 
      boolean doesNamedRefStartWithDwg = false; 
      String dwgName = "dwg_";
      if(namedRefComponent == null){
         doesNamedRefStartWithDwg = false;
      }
      else {
         String checkNamedRef = Arrays.toString((Object[]) namedRefComponent).substring(1, 5);
         if(checkNamedRef.equals(dwgName)){
            doesNamedRefStartWithDwg = true; 
         }
      } 
      return doesNamedRefStartWithDwg;
   }// end hasDwgNamedReference

Open in new window


Any Ideas?
jkteaterAsked:
Who is Participating?
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
Change line 4 above to ...

if (namedRefComponent == null || Arrays.toString((Object[]) namedRefComponent).length() < 5){

Open in new window

0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Maybe because the string representation of namedRefComponent is less than 5 characters in length? Check out the Javadoc's http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#substring(int, int) , in particular the about when it throws exceptions.
0
 
jkteaterAuthor Commented:
I think you are correct.  Most of the namedRefComponent starts with dwg_, but there are some that may not have dwg_ and have fewer than 5.  

I am trying to figure out how to check for that
0
 
jkteaterAuthor Commented:
Yep - that works great!!!  Thanks
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Not a problem, glad to help!!
0
All Courses

From novice to tech pro — start learning today.