I am trying to assign a method to a varibale called from a constructor, but it returns null when I am calling it.

public class ProjectManager {
    Calculation a = new Calculation();

    private Scanner in = new Scanner(System.in);
    private Scanner price = new Scanner(System.in);
    private Scanner duration = new Scanner(System.in);
    private String input;    
    private Calendar calendar = new GregorianCalendar();
    private SimpleDateFormat sdf = new SimpleDateFormat("dd-MMMMM-yyyy");
    private String printCal = sdf.format(calendar.getTime());
    private AdvertisementType ad = new AdvertisementType("webAdvert", "email", "radio");
    private AdvertisementType[] array = new AdvertisementType[1];


    double aa = a.getTotalPromotion();

When i try to print out "aa varibale" it gives me null value, but when I just do System.out.println(a.getTotalPromotion); the value isnt null. Is there any way to pass this method to variable and then call it.

Thank you in advance

First, some notes on your question:
You can't assign a method to an ordinary variable; you assign the result of calling that ethod.
You can't call variables, just methods.
The code you posted is not a constructor, it's jst an instance variable with an initialiser.

Anyway...
The Calculation a is created on line 2, and used on line 15. Between those there is no code that could calculate a total promotion, so at line 15 its not surprising that getTotalPromotion() returns null.

Presumably when you print a.getTotalPromotion() you are doing that somewhere else in the code - at which point the total promotion has been calculated.

ps: You created three Scanners - almost certainly a mistake. Review your notes on how to use a Scanner.

Edited 1 Year Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.