-7

I am writing a simple java code but it gave me error of NullPointerException. My code is

String s="google";
ArrayList ss[]=new ArrayList[2];
ss[0].add(s);
System.out.println(ss[0]);
Farhan
  • 404
  • 6
  • 11
  • I see the error, but you should post the stack trace. Also you should look up a null pointer exception and try to find it for your self: http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – Forseth11 May 25 '15 at 19:12

1 Answers1

0

When you create the array, you do not create each element of the array. This will work:

ss[0] = new ArrayList();
ss[0].add(s);
Eric Leibenguth
  • 3,895
  • 1
  • 18
  • 40