This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am attempting to complete an assignment for second term Java, from chapter 9 of the infamous Objects first With Java. Yup, good ol BlueJ.
So here's the deal. I need (according to my instructor) to pass an ArrayList that I created in a method of one class into the constructor of another class to make it available to a method within that class.
While I think I understand the concept, I keep running into issues with typing (as in variable type not typewriter) and the compiler not recognizing the variable in the constructor.
I'm creating the ArrayList and filling it like so:
after declaring it as a class variable like so:
That part seems to work fine, and class Game compiles OK within BlueJ. The problems occur in class TransporterRoom when I attempt to set it equal to local ArrayList I made in the constructor.
I'm attempting to pass it to the constructor in various forms none of which compile, something like:
At one point I changed the declaration to public static and got it to compile, but it did nothing. I got a NullPointerException when it was called by the method that needed it and when I poked around with BlueJ, wrote a getter in the second class and made it return the local ArrayList, I thought I was setting it equal to, it returned the correct number of Objects@someHexAddress. Otherwise I get 'static type variable where only dynamic allowed' or 'what the heck is this thing here monkey boy' or BlueJ errors to those effects.
I have searched various iterations of the subject line of this post till my browser was smokin' to no avail. I get hits but nothing that seems germane.
Any advice on how to make this work will be most humbly and gratefully accepted. I know it's something that will make me smack my head on the desk.
Please excuse any boneheaded faux pas since this is my first post 'round here, being fresh off the train from Stumptown.
Steve Stafford wrote:I need (according to my instructor) to pass an ArrayList that I created in a method of one class into the constructor of another class to make it available to a method within that class.
Try:and then wherever you need to:
TransporterRoom tr = new TransporterRoom(Game.rooms);
The problems occur in class TransporterRoom when I attempt to set it equal to local ArrayList I made in the constructor.
This may be your problem; you're not "setting it equal to", you're setting up a new TransporterRoom object that uses the rooms you've provided.
It may also be worth mentioning that if your list of rooms is likely to change, you may need to provide a copy (or snapshot) of it, so that the TransporterRoom isn't dealing with a constantly changing list.
[Edit] Actually, looking at it, Greg's suggestion is better than mine, but we're both basically saying the same thing.
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Based on what you've described as the whole purpose of the program, I would expect to see the TransporterRoom() constructor accept an ArrayList<Room> object as a parameter, either in addition to or instead of the single String it's currently expecting. Then the constructor could assign the passed ArrayList<Room> to TransporterRoom's instance variable roomArray, something like:
Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795