aspose file tools*
The moose likes Beginning Java and the fly likes Passing an ArrayList from one class as a parameter to the constructor of another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing an ArrayList from one class as a parameter to the constructor of another class" Watch "Passing an ArrayList from one class as a parameter to the constructor of another class" New topic
Author

Passing an ArrayList from one class as a parameter to the constructor of another class

Steve Stafford
Greenhorn

Joined: Mar 10, 2012
Posts: 3

Hi Folks,
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.

Thanks
Steve
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7776
    
  21

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.

Winston

[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
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 560
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
Steve Stafford
Greenhorn

Joined: Mar 10, 2012
Posts: 3

Thank you both, that cleared the fog.

I knew it would be embarassingly obvious.....

And by the way, I love this forum. Thanks again for providing a real service for us clueless n00bs

Steve
Steve Stafford
Greenhorn

Joined: Mar 10, 2012
Posts: 3

That got it working.

Thanks again for saving my GPA

S
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing an ArrayList from one class as a parameter to the constructor of another class