This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
I don't know why you are asking this, but my guess is that you are writing code in which you don't know how many variables there will be at runtime, so you are looking for a way to give them names at runtime. In a situation like that, you can use an array to store your references...
ShiftDay shiftDay = new ShiftDay[x];
...where x is determined at runtime.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Feb 08, 2008
Thanks for the reply.
This is actually for my degree project and I am stuck. This is part of an app which stores a shift rota. So Shiftday object will have to be created, when all the staff positions are set, it is stored in a vector, If i want to reference it then to retrieve it and edit it how do I know what the object reference would be if I can't set it? Hiw would I create new shiftdays?
Thanks for the reply though. I think I may be able to change the design to do it the way you have suggested.
Please use the CODE button below the message window when posting code; it makes it much easier to read.
Somehow you have managed to get an object reference which references null and tried to use it.
Look at the ShiftRota1 constructor. You have declared the field inside the constructor. Never do that. Declare it outside any methods, and instantiate it inside the constructor. Otherwise it becomes a local variable to the constructor and it shadows a field declaration and you get NullPointerExceptions. In fact when I copied and pasted your code, I got two compiler errors caused by that shadowing, so you have not posted the code which you are having problems with. Please use ctrl-C-ctrl-V to PostRealCode.
But I think a shadowing declaration may be the cause of your problem.
Joined: Feb 08, 2008
Thanks for that !
Saw the error you pointed out , changed it and got the output I wanted!