September 18th, 2004, 10:15 PM
Java/OO null String Help
Hi, I need some help with a program I'm in the process of writing. I appear to have hit a dead end. I'm not going to post the code just now unless anyone asks to see it, but I'll explain how the program works, and what my problem is.
The program (is almost pointless :P) takes a file supplied by a user and parses the file in order to create a set of menus. The menu file consists of a basic "programming language" that I've devised with three keywords (not that this part is important): "MENU", "EMENU", and "ITEM". Here's a basic sample:
I apologise for my lack of imagination (I "stole" the names of the menu from the till system at my work :P).
ITEM Stock Item
MENU Print Labels
ITEM Retail Flash Totals
MENU Till Functions
ITEM Till Function
At the moment, the menu system itself consists of three files, MenuItem.java, Menu.java and Item.java. MenuItem is the superclass of Menu and Item like so:
MenuItem contains some basic info like a name, a method to get the name, a bool that tells you whether the class in question is a Menu or Item and a method isMenu() (returns the bool). It's really only there because they share the majority of a constructor and the name and bool, and also because Menu contains an array of MenuItems (which allows you to place either Menu objects or Item objects in the array)
The constructor for Item pretty much runs the superclass constructor (which truncates the name if necessary for cosmetic reasons) and sets the bool to false (cause it's not a menu).
The constructor for Menu runs the superclass constructor, sets it's bool to true and then runs a method called fillMenu() which parses the menu file and creates menus and items appropriately and adds them to it's MenuItem array. (starts to get complex with recursions and whatnot )
The problem I'm getting is that when I call getName() on a menu, I get it's name fine, but when I call it on an Item, the String returned is null. This is slightly strange because the getName() method is defined in the superclass.
And while writing that last sentence, I've suddenly realised that I forgot to take getName() out of the Item class and it overrided the definition in the MenuItem class. As soon as I took it out, the program started to work again.
I'm still posting this message for the sake of sharing my experience. It might interest someone.
Another reason for having a superclass MenuItem was because I didn't just want to use an array of Objects and basically allow anything into the array (although with the currect design that isn't a problem)[/edit]