• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting a Null pointer exception at this point

 
Deepika Namasani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

can you please let me know y am i getting null pointer exception at this point

" ar[k2-1].add(new Integer(input[i]));"

from the following code.

 
Ryan McGuire
Ranch Hand
Posts: 1068
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because you have allocated an array of three references to ArrayLists, but you haven't instantiated the actual ArrayList objects yet.

Here is the new code. I've taken the liberty of reworking how input[] is indexed in the first set of nested loops.



Cool?
[ June 24, 2005: Message edited by: Ryan McGuire ]
 
Deepika Namasani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh thanks
yah that works
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan please can you tell me why do we need to create a new ArrayList object, so that we don't get a NullPointerException.


Havn't we initialised ar[] prior to the above line of code
 
Deepika Namasani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abdulla


I created Array of ArrayList

actual ArrayList syntax is ArrayList a = new ArrayList().


So i created an array of ArrayList
ie



i created array size of 3 each of type arrayList so at this point
ar[0],ar[1],ar[2] are just variables of type ArrayList are created but are pointing to null .
so until unless we redirect this from null to actual Arraylists they will be with null. and accessing a null object produces null pointer exception.

by assiginig
where k2 = 0,1,2

we are redirecting the variable to ArrayList object.

I hope i cleared ur doubt.

thanks
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Deepika
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic