Problem with arraylist

so, i have been attempting to create a neural network in processing, but after i get the error: ArrayIndexOutOfBoundException: Index 0, Size:0, i can not think of any way that this is caused

ArrayList<Float>generateNodes(int layer,int nodes) {
  // generates IndexOutOfBounds ArrayList[] layers=new ArrayList[layer+1];
  // same error as line 70 ArrayList[] layers={new ArrayList<ArrayList<Float>>(),new ArrayList<ArrayList<Float>>(),new ArrayList<ArrayList<Float>>()};
  ArrayList<ArrayList<Float>> layers=new ArrayList<ArrayList<Float>>();
  ArrayList<ArrayList<Float>> InternalNodes=new ArrayList<ArrayList<Float>>();
    if(layer==1) {
      for(int l=0; l<=inputs.length-1; l++) {
       layers.add(new ArrayList<Float>(Arrays.asList(node(inputs[l],weights.get(l)))));  
      }
    }
    else {
      for(int n=0; n<=nodes; n++) {
    layers.add(new ArrayList<Float>(Arrays.asList(node(InternalNodes.get(layer-1).get(n),weights.get(weights.size()-1)))));
      }
    }
   return layers.get(layers.size()-1);
  }
}

If the size were 1, there would be one object in the ArrayList, and that one item would be at index 0.

But the size is 0, not 1. So there isn’t even one item in the ArrayList. So there’s nothing at index 0. Because if there was something at index 0, the size would be 1, not 0.

Which line are you getting the error on? Chances are good that you are trying to access - possibly return - the 0th item in an empty ArrayList. I mean, look at the return statement… Are you checking that layers has an item in it before you try to get the last one?

1 Like

you where correct, thank you , yes, the problem was that the line that was supposed to initialize the arraylist only triggered when a boolean was set to true, and this boolean was false by default, thankyou

1 Like