Sorry for the misinformation. I really should have run your code to see where the nullPointerException occurs.
The nullPointerException occurs, because al[p] is null (and not the value given to the add-method what I assumed wrongly)
You can see that if you add following line right before the error at line 33:
- System.out.println("al length=" + al.length + " p=" + p + ((p < al.length) ? " al[p]=" + al[p] : " invalid index"));
The output is:
- al length=3 p=0 al[p]=null
That means, you have defined an array of arrayLists.
But you have not initialized its content, that means you have not put anything inside your array of arrayLists. There is everywhere "null" inside. You should put empty arrayLists inside to initialize its content (after line 13):
- for(int index = 0;index < al.length;index++)
-
{
-
al[index]=new ArrayList<Object>();
-
}
-
Try it and see that your null pointer exception is away. (but it can have IndexOutOfBounds-Exception if "p" is equal or bigger than the given array size n+1)
In my understanding that's a design error. You only wanted a single arrayList.
if that assumption is correct, then you should use in line 13:
- ArrayList<Object> al = new ArrayList<Object>();
And then in line 33:
- al.add(new Integer(a[j]));