• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting a Null pointer exception at this point

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh thanks
yah that works
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Deepika
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic