Hii,I was trying to solve a problem but i don't get why my program is showing null pointer exception on the commented line.Can someone explain the reason.
Code:
import java.util.*;
class cod1{
public static void main(String args[]) throws java.lang.Exception
{
int t,i,n,j,k,p,q,r;
int[] a =new int[100000];
Scanner s = new Scanner(System.in);
t=s.nextInt();
for(i=0;i<t;i++)
{
p=0;
n= s.nextInt();
ArrayList<Object>[] al = (ArrayList<Object>[])new ArrayList[n+1];
for(j=0;j<n;j++)
{
a[j]=s.nextInt();
}
int c;
for(j=0;j<n;j++)
{
c=0;
for(k=0;k<p;k++)
{
if(al[k].size()-1>a[j])
{
al[k].add(a[j]);
c++;
break;
}
}
if(c==0)
{
al[p++].add(a[j]);//null pointer exception
}
}
for(j=0;j<p;j++)
{
System.out.println(al[j].size()-1);
}
}
}
}
Comment