-1

How can I fix the following exception java.lang.ArrayIndexOutOfoundsExcpetion. Exception and the source code are given below.

Exception:

java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 1 at String s[] = data.split(",");

Source code:

            for (String data : temp) {
                String s[] = data.split(",");
                if (data.startsWith("AppM")) {
                    application.addAppModule(s[1], Integer.parseInt(s[2]), Integer.parseInt(s[3]));
                } else if (data.startsWith("TUPLE")) {
                    application.addTupleMapping(s[1], s[2], s[3], new FractionalSelectivity(1.0));
                    AppLoop loop = new AppLoop(new ArrayList<String>(){{add(s[2]);add(s[1]);add(s[3]);}});
                    loops.add(loop);
                } else {
                    String[] ss = s[3].split("\\*");
                    application.addAppEdge(s[0], s[1], Double.parseDouble(s[2]),
                            (Double.parseDouble(ss[0])*(Double.parseDouble(ss[1]))), s[4],
                            checkTupleDirection(s[5]), checkAppEdgeDirection(s[6]));
                }
            }
             

How can I fix this error?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
sandeepa
  • 21
  • 5
  • 2
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – OH GOD SPIDERS Oct 20 '20 at 10:04

3 Answers3

0

I think you mean you're getting the error here...

String[] ss = s[3].split("\\*");

Maybe you could add a check after String s[] that checks the size of the array and don't assume that there is anything at s[3].

TedTrippin
  • 3,525
  • 5
  • 28
  • 46
0

In your case, the splitted array did not have enough items and when you try to access an item using the index > length of the array, ArrayIndexOutOfBounDException naturally occurs.

Its a good practice to check for the length of the array before accessing them using index, like below sample code, and remember the array uses the zero based index. To access the first element you should use the index 0.

    int expectedLength = 3; 
        if(s.length ==expectedLength){
                        application.addAppModule(s[0], Integer.parseInt(s[1]), Integer.parseInt(s[2]));
                    } else{
// data may not be processable. 
} 
Prabhu
  • 129
  • 1
  • 1
  • 9
0

Problem is you are trying to access an array index which is not there in your array.That's why you are having this exception.So at the point you are getting the exception your array doesn't contain an element index 3 ,so when you are trying to access that element index which is not already there program is throwing an exception.So to fix this issue you can have a if conditional statement like below.

Refactor the code like below.

 for (String data : temp) {

         String s[] = data.split(",");
         if(s.length == 3){ //checking whether your array contains 3 elements before accessing the elements  
                   
                if (data.startsWith("AppM")) {
                    application.addAppModule(s[1], Integer.parseInt(s[2]), Integer.parseInt(s[3]));
                } else if (data.startsWith("TUPLE")) {
                    application.addTupleMapping(s[1], s[2], s[3], new FractionalSelectivity(1.0));
                    AppLoop loop = new AppLoop(new ArrayList<String>(){{add(s[2]);add(s[1]);add(s[3]);}});
                    loops.add(loop);
                } else {
                    String[] ss = s[3].split("\\*");
                    application.addAppEdge(s[0], s[1], Double.parseDouble(s[2]),
                            (Double.parseDouble(ss[0])*(Double.parseDouble(ss[1]))), s[4],
                            checkTupleDirection(s[5]), checkAppEdgeDirection(s[6]));
                }
        }
}
             
Hasindu Dahanayake
  • 1,344
  • 2
  • 14
  • 37