So I am initializing an ArrayList of ArrayLists in order to have a set of resizable Arrays to hold the names of classes as strings for a Design Analyzer homework assignment. The ArrayLists are initialized to a size of 7 (For the test file that I am using), yet when I perform a get on element 1, I am getting an IndexOutOfBounds exception. Upon checking the size of the ArrayList causing the problem (providers), the size is zero. I am having a hard time understanding why the ArrayList is size zero, despite the fact that I have initialized it to be the size of my cls ArrayList (which is 7 in my test case). The exception is being thrown when I attempt the get(i) on providers in my if statement, but why? Any help would be appreciated.
public class DesignAnalyzer {
//private Hashtable classSet = new Hashtable();
//ArrayList<Integer> counters = new ArrayList<>();
private static ArrayList<ArrayList<String>> providers;
private static ArrayList<ArrayList<String>> clients;
//private static ArrayList<String>[]
public static void analyze(ArrayList<Class<?>> cls, String path){
Package homePkg = Package.getPackage(path.substring(path.lastIndexOf("\\")+1));
ArrayList<ArrayList<String>> providers = new ArrayList<ArrayList<String>>(cls.size());
ArrayList<ArrayList<String>> clients = new ArrayList<ArrayList<String>>(cls.size());
providers.ensureCapacity(cls.size());
clients.ensureCapacity(cls.size());
for(int i = 0; i < cls.size(); ++i){
providers.set(i, new ArrayList<String>());
clients.set(i, new ArrayList<String>());
}
getProviders(cls, homePkg);
getClients(cls);
//providers.clear();
}
private static void getProviders(ArrayList<Class<?>> cls, Package pkg){
for(int i = 0; i < cls.size(); ++i){
Class<?> spr = cls.get(i).getSuperclass();
int temp = providers.size(); //should be 7 in test case, but coming back as zero
if(spr != null && spr.getPackage().toString().equals(pkg.toString()) &&
!providers.get(i).contains(spr.toString())) // exception being thrown here at i = 1 b/c providers.size is zero...
providers.get(i).add(spr.toString());