This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Anonymous Inner Class Design  RSS feed

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm trying to do something like the following:



The goal here is to have different implementations of the getList() method return different lists BUT, without writing new classes for each implementation. This way I'd be able get the list I want by writing



The thing of it is tho' is that it doesn't work. Everything compiles fine, so I'm not doing anything "illegal" per se, but I get NullPointerExceptions when the class is initialized and I'm not sure why. Does it have something to do with the static variables being intialized before other stuff? The other idea I had was to write private inner classes that extend Category and have the static variables return new instances of them. It just doesn't seem as "elegant." Anyway - does anyone have any ideas either to get this to work or somethign similar. Thanks!
 
Brian R. Wainwright
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok... I should retitle this post "Stupid Developer Tricks. For anyone who cares, this DOES work - what doesn't work is some other code I didn't post.

--BW
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing wrong with what you've shown here -- the problems must be in code you've simplified away.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For future reference, it will help if you include the COMPLETE error message with your posts. By paraphrasing, you lose critical information (such as the file name and line number) that is given with the stack trace for any Java exception. These two pieces of information can help us (or even yourself) track down the cause of the error. So when you have questions in the future, please be sure to include the full stack trace (well, the first dozen lines, or so, at least).

BTW, I'm glad you figured it out

Keep Coding!

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!