Results 1 to 17 of 17
- 01-16-2012, 08:13 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
Calling a method from another class? *confused and stressed*
I am trying to call an instance of a class from another class (an instance of that).
It goes as follows (to sum it up)
Java Code:Warehouse (a class) public ArrayList<Item> warehouse;
Now what I need to do is to access the ArrayList from another class.Java Code:(Constructor) warehouse = new ArrayList<Item>();
I've tried doing the following in the other class but to no avail (I'm sure it's a logical thinking error on my part)
So to sum it up I have three classes. A warehouse class which makes an ArrayList to contain instances of items in it. And an order class that needs to access said arraylist so that I can add the numbers of an item to an order for printing. Makes sense? :)Java Code:Order public void addItem(long itemNo) { Item item = warehouse.returnArrayList; item.getItem(nr) long itemNo = item.getNo(); this.wareho use.add(itemNo); }
- 01-16-2012, 08:54 PM #2
Re: Calling a method from another class? *confused and stressed*
One way is to have the class containing the ArrayList have a get... method that returns a reference to the ArrayList when called. Then any class that has a reference to that class containing the AL can call the get method and receive a reference to the AL that it can use.need to do is to access the ArrayList from another class.
- 01-16-2012, 08:59 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
Re: Calling a method from another class? *confused and stressed*
You mean something along the lines of
I've already got one like that in the Warehouse class - I seem to have forgot to mention that in the first post. I'm already trying to connect to that via the Item item method in the Order class. This returns a "cannot find symbol - variable warehouse" error - which is where my logic is currently failing. I've got a deadline for tomorrow morning so my logical thinking is at an all time low. Lemme copy paste the classes in their entirety if that helps. (Will take a sec)Java Code:public ArrayList<Item> returnArraylist() { return warehouse; }
- 01-16-2012, 09:02 PM #4
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
Re: Calling a method from another class? *confused and stressed*
The code is in Danish, so apologies for that. Warehouse/Varelager and Item/Vare.
.gif)
Ninjaedit: To summarise once more in case I've managed to fail at that, I need the Order class to fetch the Arraylist of Item from the Warehouse class, and then perform item.getNo() on them so I can add the returned value (every item has an itemNo in the system) to a receipt - one item can be added several times since it's an ordering system.
Varelager
OrdreJava Code:package ModelLag; import java.util.ArrayList; /** * Varelager * Agerer container for alle instanser af typen vare i programmet. * */ public class Varelager { public ArrayList<Vare> varelager; // Instansvariabler long nr; // Holder styr på hvilken vare der skal mnaipuleres. Vare vare; /** * Constructor */ public Varelager() { varelager = new ArrayList<Vare>(); } // Tilføj ny vare public void newVare(long nr, String type, String lokation, double pris, double rabatPct, String beskrivelse, long beholdning, long minBeholdning, long maxBeholdning) { Vare vare = new Vare(nr, type, lokation, pris, rabatPct, beskrivelse, beholdning, minBeholdning, maxBeholdning); this.varelager.add(vare); } // GET vare public Vare getVare(long nr) { int index = 0; while(varelager.size()> index) { vare = varelager.get(index); if(vare.getNr() == (nr)) { return vare; } index++; } return null; } // SET metoder for vare(r) public void setNr(long oldNr, long nr) { vare = getVare(oldNr); vare.setNr(nr); vare = null; } public void setType(long nr, String type) { vare = getVare(nr); vare.setType(type); vare = null; } public void setLokation(long nr, String lokation) { vare = getVare(nr); vare.setLokation(lokation); vare = null; } public void setPris(long nr, double pris) { vare = getVare(nr); vare.setPris(pris); vare = null; } public void setRabatPct(long nr, double rabatPct) { vare = getVare(nr); vare.setRabatPct(rabatPct); vare = null; } public void setBeskrivelse(long nr, String beskrivelse) { vare = getVare(nr); vare.setBeskrivelse(beskrivelse); vare = null; } public void setBeholdning(long nr, long beholdning) { vare = getVare(nr); vare.setMinBeholdning(beholdning); vare = null; } public void setMinBeholdning(long nr, long minBeholdning) { vare = getVare(nr); vare.setMinBeholdning(minBeholdning); vare = null; } public void setMaxBeholdning(long nr, long maxBeholdning) { vare = getVare(nr); vare.setMaxBeholdning(maxBeholdning); vare = null; } public void deleteVare(long nr) { vare = getVare(nr); this.varelager.remove(vare); vare = null; } public ArrayList<Vare> returnerLager() { return varelager; } }
Java Code:package ModelLag; import java.util.ArrayList; import ModelLag.Varelager; /** * Ordre * indeholder en instans af typen kunde(telefonNr, navn, ...) */ public class Ordre { // instansvariabler private long ordreNr; private int tid; private int status; private Kunde kunde; private Rabatgruppe rabatgruppe; private ArrayList<Long> vareliste; // Constructor & Add metode public Ordre(long ordreNr, int tid, int status, Kunde kunde) { this.ordreNr = ordreNr; this.tid = tid; this.status = status; this.kunde = kunde; } public void addVare(long nr) { Vare vare = varelager.returnerLager; vare.getVare(nr); long vareNr = vare.getNr(); this.vareliste.add(vareNr); } // GET metoder. public long ordreNr() { return ordreNr; } public int getStatus() { return status; } // SET Metoder. public void setStatus(int status) { this.status = status; } }Last edited by Illanair; 01-16-2012 at 09:04 PM.
- 01-16-2012, 09:15 PM #5
Re: Calling a method from another class? *confused and stressed*
Please post the full text of the error message.
What does the method: returnerLager return?
What does this line do with what is returned:
Vare vare = varelager.returnerLager;
- 01-16-2012, 09:21 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
Re: Calling a method from another class? *confused and stressed*
I'm using BlueJ to write in (course requirement) so the error is just "cannot find symbol - variable varelager".
returnerLager returns the ArrayList of Vare (item) - that is the entire list of items added to the warehouse.
Vare vare = varelager.returnerLager; - is supposed to make a local variable so that I can perform methods such as long vareNr = vare.getNr(); within the Order class to get the number stored within an instance of Vare (long nr) and add the returned number to the vareliste arraylist (an array of long values)
The error is related to the whole "I can't seem to speak with the Varelager class to make it accept method calls" issue. I need some way of making Order call a method (returnerLager in this case) in the varelager (Warelager) instance, so I can use the information stored within its ArrayList<Vare>
- 01-16-2012, 09:26 PM #7
Re: Calling a method from another class? *confused and stressed*
Where is the variable: varelager defined? The compiler does not think that variable is in scope where you are trying to use it. In scope means it is defined within the same enclosing pair of {}s where it is being referenced.cannot find symbol - variable varelager
- 01-16-2012, 09:39 PM #8
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
Re: Calling a method from another class? *confused and stressed*
The "variable" is defined as an ArrayList in the start of the Varelager class. So the issue is that my method in the order class cannot find the variable from within the Varelager class. I need to establish some sort of link to it.
To avoid confusion here's a quick overview of the application - it is layer based (UI > Controller > Model)
The UI creates an instance of the Controller, which creates an instance of Varelager. I need to "tap in" to the instance that my controller uses for varelager (to avoid making another "new varelager instance" method call)
Note: varelager is the name for two different things. In the Controller class/instance it is used as the name for the instance of the Varelager class - in the Varelager instance it is used as a name for an Arraylist (Bad design I know, but I have to keep it readable)
Does any of this help - or just add further confusion? I'm sorry to be such a bother.
- 01-16-2012, 09:53 PM #9
Re: Calling a method from another class? *confused and stressed*
There is some confusion then.The "variable" is defined as an ArrayList
You use the variable: varelager as a class reference to access this variable: returnerLager in the statement:
But returnerLager is a method in the Varelager class:Java Code:Vare vare = varelager.returnerLager;
To call a method the correct syntax would be to add () at the end of the name:Java Code:public ArrayList<Vare> returnerLager()
However the method does not return a Vare, it returns: ArrayList<Vare>Java Code:Vare vare = varelager.returnerLager();
So this code is very confused.
Also somewhere you need to define the variable: varelager as a reference to the Varelager class:
AND you need to give it the value of an instance of the Varelager classJava Code:Varelager varelager ...
- 01-16-2012, 10:01 PM #10
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
Re: Calling a method from another class? *confused and stressed*
Point taken. I'll have that fixed.
As for the last bit with the referencing, how do I do this exactly? So I can start calling methods from the "varelager" instance?
The varelager instance of Varelager is defined within an instance of VareCtr (aptly named vareCtr, to fit the uppercase/lowercase pattern).
How do I make it do the "Varelager varelager = "instance varelager within instance of instance of VareCtr, vareCtr"? This is the confusing bit for me right now.
I've tried googling the subject but I can't seem to find a solution that makes sense to me.
- 01-16-2012, 10:06 PM #11
Re: Calling a method from another class? *confused and stressed*
You need to consider how to get a reference of the VareLarger class to the Ordre class.
Where are there instances of the two classes created? Pass a reference to the VareLarger class to the Ordre class when you create it. Perhaps something like this:
Java Code:Var v = new Var(); // create Var class Ord o = new Ord(v); // Create Ord class and pass it a reference to the Var class
- 01-16-2012, 10:17 PM #12
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
Re: Calling a method from another class? *confused and stressed*
Wont the "new Var();" make a new instance of the class though?
- 01-16-2012, 10:22 PM #13
Re: Calling a method from another class? *confused and stressed*
Yes it will. That is only a simple example.
You will have to decide how to pass the reference to the Var class to the Ord class.
- 01-16-2012, 10:28 PM #14
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
Re: Calling a method from another class? *confused and stressed*
Right - just wanted to make sure.
This a diagram of how the program looks. The UI creates instances of the two controllers, who then each make an instance of their "child", who then create instances as needed.
So to answer your question, the VareCtr class is responsible for creating the instance "varelager", and the class "OrdreList" is responsible for creating the Ordre class (which is the one that needs access to the single(ton) varelager instance.
I can visualise the link, but I can't seem to express it in code to make the connection from an instance of Ordre to the single varelager instance.

Edit: I know I'm not making much sense - thanks for bearing with me thus far.
- 01-16-2012, 10:31 PM #15
Re: Calling a method from another class? *confused and stressed*
The problem is having a variable in one class and needing to get to it from another.
To do that, the second class needs a path/reference for the first class so it can call a method in the first class. If there is no way with your design, then you have to change the design.
- 01-16-2012, 10:46 PM #16
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
Re: Calling a method from another class? *confused and stressed*
I'll give it a rest and try and redesign the code tomorrow to allow for the referencing. Maybe my teacher has some good suggestions seeming as he made the core design frame for us to use.
Thanks a lot for all the help. It has been invaluable.
- 01-16-2012, 10:47 PM #17
Similar Threads
-
Calling a method in another class
By uncopywritable in forum New To JavaReplies: 9Last Post: 10-22-2012, 04:01 PM -
Calling a class and a little confused
By Boomer1 in forum New To JavaReplies: 1Last Post: 12-29-2009, 06:10 PM -
Child-Class Calling a Method in a Parent-Class
By Blah_ in forum New To JavaReplies: 5Last Post: 09-29-2009, 02:48 AM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks