0

I wrote following recursive function it is throwing an exception java.lang.ArrayIndexOutOfBoundsException: 2. It is working for small structure but while I am trying to give a part number which is having large structure exception is coming.

I call the function using recursionTest(1, stub, partNo, list);

Code is:

public static void recursionTest(int level, SoapBindingStub stub, 
         String partNumber, List<Part> list) throws RemoteException {

    String children = (String) stub.getPartDelegate(partNumber);

    String[] parts = children.split(",");

    if(parts.length == 1)
        return;

    for(int i = 1; i < parts.length; i++) {

        String[] childDetail = parts[i].split(":");

        list.add(setPartAttributes(level, childDetail));

        recursionTest(level + 1, stub, childDetail[0], list);

    }
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
Ruchita
  • 23
  • 1
  • 9
  • 1
    Please show the relevant portion of your stacktrace – Scary Wombat Jun 29 '17 at 07:44
  • I am storing the information about part in one string which contains name number etc. So to split this attributes I used ' : ' in program. So while printing and storing this information I will need ' : ' to separate those attributes. – Ruchita Jun 29 '17 at 07:46
  • java.lang.ArrayIndexOutOfBoundsException: 2 at com.task.controllers.PartRestController.setPartAttributes(PartRestController.java:66) ~[classes/:na] at com.task.controllers.PartRestController.recursionTest(PartRestController.java:53) ~[classes/:na] at com.task.controllers.PartRestController.recursionTest(PartRestController.java:55) ~[classes/:na] at com.task.controllers.PartRestController.recursionTest(PartRestController.java:55) ~[classes/:na] at com.task.controllers.PartRestController.getParts(PartRestController.java:34) ~[classes/:na] – Ruchita Jun 29 '17 at 07:54
  • so the problem is at `setPartAttributes(Pa‌​rtRestController.jav‌​a:66)` not with the code you have posted – Scary Wombat Jun 29 '17 at 07:57
  • So the error is not in the code you're showing but in the setPartAttributes method you're **calling** from the code shown! – sruetti Jun 29 '17 at 07:57
  • 60 public static Part setPartAttributes(int level, String[] detail) { 61 62 Part part = new Part(); 63 part.setLevel(level); 64 part.setNumber(detail[0]); 65 part.setName(detail[1]); 66 part.setLineNumber(detail[2]); 67 part.setQuantity(detail[3]); 68 part.setReferenceDesignator(detail[4]); 69 part.setUnit(detail[5]); 70 71 return part; 72 } – Ruchita Jun 29 '17 at 08:03
  • Ok, with that context the "mark as duplicate" is actually justified... On line 66 you're accessing index 2 of `detail` and you get an exception telling you that index 2 is out of bounds. So you're `detail` array obviously does not have the 6 elements needed for this method to work, as `parts[i].split(":");` doesn't return 6 or more elements. – sruetti Jun 29 '17 at 08:21

0 Answers0