简体   繁体   中英

Main program not recognizing my class method java

I asked a question about this previously but I wasn't able to resolve it. I'm trying to create an array of Supplements (class) and store it in a single object of Magazine (class). When trying to call my fillArray method, I get the error:

cannot find symbol: 
symbol: method fillArray(Supplement[])
location: javaapplication1

What could be the issue with this?

Supplement.java :

//
    //
    //  Generated by StarUML(tm) Java Add-In
    //
    //  @ Project : Untitled
    //  @ File Name : Supplement.java
    //  @ Date : 21/04/2020
    //  @ Author : 
    //
    //

    package javaapplication1;


    public class Supplement {
        private String supplementname;
        private int WeeklySupCost;

            public void fillArray(Supplement[] supplements2){


                supplements2[0] = new Supplement("Sports Illustrated Special", 4);
                supplements2[1] = new Supplement("Health and Nutrition", 2);
                supplements2[2] = new Supplement("Lifestyled", 5);
                supplements2[3] = new Supplement("Gamer's Update", 3);


             };

            public void SetSupplementName(String supplementname1){

                supplementname = supplementname1;

            };
        public void WeeklySupCost(int WeeklySupCost1){

                WeeklySupCost = WeeklySupCost1;

            };
        public String GetSupplementName(){

                return supplementname;

            };
        public int GetWeeklyCost(){

                return WeeklySupCost;

            };
        public void SetSupplement(String supplementname1, int WeeklySupCost1){

                supplementname = supplementname1;

            };
        public Supplement(String supplementname1, int WeeklySupCost1){

                SetSupplement(supplementname1, WeeklySupCost1);

            };

            public Supplement(){};
    }

Magazine.java :

    //
    //
    //  Generated by StarUML(tm) Java Add-In
    //
    //  @ Project : Untitled
    //  @ File Name : Magazine.java
    //  @ Date : 21/04/2020
    //  @ Author : 
    //
    //
    package javaapplication1;

    public class Magazine {
        private String magazinename;
        private int WeeklyCost;
        private Magazine magazineobj;
        private Supplement[] supplements;

        public void SetMagazineName(String magazinename1){

            magazinename = magazinename1;

        };
        public void SetWeeklyCost(int WeeklyCost1){

            WeeklyCost = WeeklyCost1;

        };
        public String GetMagazineName(){

            return magazinename;

        };
        public int GetWeeklyCost(){

            return WeeklyCost;

        };
        public void SetMagazine(String magazinename1, int WeeklyCost1, Supplement[] supplements1){

            magazinename = magazinename1;
            WeeklyCost = WeeklyCost1;
            supplements = supplements1;

        };
        public Magazine(String magazinename1, int WeeklyCost1, Supplement[] supplements1){

            SetMagazine(magazinename1,WeeklyCost1,supplements1);

        };

        public Magazine(){};
        public void printMagazine(){

            System.out.println(magazineobj);

        }
    }

Main program:

package javaapplication1;

public class JavaApplication1 {
    public static void main(String[] args) {


        Supplement[] supplements = new Supplement[4];

        fillArray(supplements);

        Magazine magazineobj = new Magazine("The Wheels Special", 35, supplements);
        magazineobj.printMagazine();

    }

}

You need to make the method fillArray static to access it without an instance of Supplement because in the JavaApplication1 class is no method called fillArray is static or can be accessed by a static context.

Use:

public static void fillArray(Supplement[] supplements) { ... }

Call:

Supplement.fillArray(suplements);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM