• Post Reply Bookmark Topic Watch Topic
  • New Topic

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: 24217
38
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!