Results 1 to 17 of 17
  1. #1
    Illanair is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default 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;
    Java Code:
    (Constructor)
    warehouse = new ArrayList<Item>();
    Now what I need to do is to access the ArrayList from another class.

    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)

    Java Code:
    Order
    public void addItem(long itemNo)
    {
        Item item = warehouse.returnArrayList;
        item.getItem(nr)
        long itemNo = item.getNo();
        this.wareho
        use.add(itemNo);
    }
    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? :)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Calling a method from another class? *confused and stressed*

    need to do is to access the ArrayList from another class.
    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.

  3. #3
    Illanair is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Calling a method from another class? *confused and stressed*

    You mean something along the lines of

    Java Code:
    public ArrayList<Item> returnArraylist()
        {
            return warehouse;
        }
    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)

  4. #4
    Illanair is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Calling a method from another class? *confused and stressed*

    The code is in Danish, so apologies for that. Warehouse/Varelager and Item/Vare.

    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
    Java 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;
        }
    }
    Ordre
    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.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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;

  6. #6
    Illanair is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default 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>

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Calling a method from another class? *confused and stressed*

    cannot find symbol - variable varelager
    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.

  8. #8
    Illanair is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default 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.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Calling a method from another class? *confused and stressed*

    The "variable" is defined as an ArrayList
    There is some confusion then.
    You use the variable: varelager as a class reference to access this variable: returnerLager in the statement:
    Java Code:
     Vare vare = varelager.returnerLager;
    But returnerLager is a method in the Varelager class:
    Java Code:
    public ArrayList<Vare> returnerLager()
    To call a method the correct syntax would be to add () at the end of the name:
    Java Code:
    Vare vare = varelager.returnerLager();
    However the method does not return a Vare, it returns: ArrayList<Vare>

    So this code is very confused.

    Also somewhere you need to define the variable: varelager as a reference to the Varelager class:
    Java Code:
    Varelager varelager ...
    AND you need to give it the value of an instance of the Varelager class

  10. #10
    Illanair is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default 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.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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

  12. #12
    Illanair is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Calling a method from another class? *confused and stressed*

    Wont the "new Var();" make a new instance of the class though?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.

  14. #14
    Illanair is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default 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.
    Attached Thumbnails Attached Thumbnails Calling a method from another class? *confused and stressed*-helpdesk.png  

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.

  16. #16
    Illanair is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default 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.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Calling a method from another class? *confused and stressed*

    Sorry you did not get it. Asking your teaching is probably a good idea.

Similar Threads

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 04:01 PM
  2. Calling a class and a little confused
    By Boomer1 in forum New To Java
    Replies: 1
    Last Post: 12-29-2009, 06:10 PM
  3. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •