0

I'm trying to run my class file which has a main method in Netbeans, and I get this error.

Exception in thread "main" java.lang.NullPointerException
    at javaapplication1.Magazine.main(Magazine.java:25)

What's the issue? For clarity, each Magazine has an some Supplements so I have tried to create an array of Supplements to store in one instance of a magazine.

I also don't fully understand the reason behind using a main method in a class. How would I make use of it in the main program?

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 Supplement[] supplement;
        private Magazine magazineobj;


        public static void main(String args[]){

            Supplement[] supplements = new Supplement[3];
            for(Supplement supplement: supplements){

                supplement.fillArray();

            }

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


        };

        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;
            supplement = supplements1;

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

            SetMagazine(magazinename1,WeeklyCost1,supplements1);

        };

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

            System.out.println(magazineobj);

        }
}

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;

        Supplement[] supplements = new Supplement[3];

        public void fillArray(){


            supplements[0] = new Supplement("Sports Illustrated Special", 4);
            supplements[1] = new Supplement("Health and Nutrition", 2);
            supplements[2] = new Supplement("Lifestyled", 5);
            supplements[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(){};

}

Giancarlo Romeo
  • 663
  • 1
  • 9
  • 24
thedafferg
  • 99
  • 7
  • You're trying to call a method `.fillArray()` on a reference `supplement` that is null – khelwood Apr 22 '20 at 08:04
  • There is another error *also*: You create Supplement[] supplements = new Supplement[3]; but you access to supplements[3]. For an array of 3 elements, the indexes go from 0 to 2. – Giancarlo Romeo Apr 22 '20 at 08:06
  • @khelwood So how would I be able to make it a method that fills up the Supplement array in the magazine class? – thedafferg Apr 22 '20 at 08:19
  • If you want to write a method to create Supplements in an array, that should not be an instance method of a `Supplement` class. You could make it a static method and pass the array into it; that would make more sense. – khelwood Apr 22 '20 at 08:26

0 Answers0