This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Calling seperate functions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling seperate functions" Watch "Calling seperate functions" New topic
Author

Calling seperate functions

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Hi All. I'm passing class ship to the GUI, using GUI(Ship ship) but can't refer to it later in the code:

Main:


GUI:


Ship:

Any ideas?

Rob.
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

You need to pass the ship object to the method somehow.
Either by a field or by parameter.


Please correct my English.
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
I have:
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

So far so good. But don't just ignore it. Take it and store it somewhere so you can use it later.

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Thanks Paul.

Thing is i want to call the functions of each instance of class Ships. I could pass the ArrayList ships to the GUI but is there an easier way?

At the moment all that's happening is i'm calling hit on an empty ship.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
You need to distinguish passing a Ship from a List<Ship>. Both are possible, but they are very different from each other.

By an "empty ship", do you mean a null reference? You should avoid nulls as far as possible, as in the following example. Note the robust reaction to a null, and the bit about nulls in the documentation comment.
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
At the moment i'm passing an empty instance of Ship to the GUI thread.

I'm using a static method to cycle through each ship in the ArrayList Ships calling the method hit() on each of them. Is there an easier way?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
What do you mean by "empty instance"?
Why are you using a static method?
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
I'm creating a ship without running setShips on it, therefore it's x and values are all zero.

I'm creating a static method hits which cycles through each member of the static arraylist ships and calls the method hits on it:
 
wood burning stoves
 
subject: Calling seperate functions