• 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

linked lists

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having a problem implementing a linked list... It's contained in an object, and after instantiating the object,
Highway h = new Highway(27.78,10000.0);
The linked list inside it should be instantiated (I think), this is inside the constructor for Highway...
LinkedList cars = new LinkedList();
The problem I'm having is that afterwars my linked list always appears to be null, even if I add something to it... and whever I try to use any of the methods of the linked list I get a Null Pointer Exception...
If anybody's got any ideas, I'd appreciate some help, thanks.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your "cars" reference may be getting clobbered somewhere else in your code. Try looking for all references to "cars" and any assignments to it to make sure it isn't getting set to null.
If you still have problems, try posting some code (try to post only relevant pieces though ...some folks will post 500 LOC and expect others to read through all of it :roll: ...NOT a thing to do here)
HTH,
Junilu
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kevin,
Welcome to JavaRanch!
My best guess is that you are declaring and instantiating a LinkedList inside your constructor only and as such this LinkedList doesn't exist outside of the constructor.
Perhaps you could post some example code in the simplest form possible to replicate your problem.
Good Luck.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you doing this:
 
Kevin Girard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, that looks like it exactly, here's my code, in brief

[ May 22, 2002: Message edited by: Kevin Girard ]
 
Kevin Girard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did some testing, and I can use the list inside the constructor, but anywhere else, it's null... so I'm pretty sure that backs up what you guys were saying.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then do this:

Do you understand why?
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do, I will answer for you. DOH!
 
Kevin Girard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DOH is right! Thanks guys
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic