alxmke
alxmke

Reputation: 334

Compilation error when attempting to call methods which define a variable from another class

These two classes are described in depth in the javdocs but in short: it is meant to calculate the CO2 emissions from electricity use. However I am returning an error when attempting to compile that reads as follows:

non-static method calcAverageBill(java.util.ArrayList<java.lang.Double>) cannot be referenced from a static context.

I'm not certain what the issue is, could anybody help?

public class CO2fromElectricity
{
    private double monthBillAverage;                           
    private double monthPriceAverage;                          
    private double annualCO2emission;                         
    private double emissionFactor;                             
    private double months;                                    

    CO2fromElectricity() { }

    public double calcAverageBill(ArrayList<Double> monthlyBill)
    {
        monthBillAverage = 0;
        for(double billToken : monthlyBill)
        {
            monthBillAverage += billToken;
        }
        return monthBillAverage / monthlyBill.size();
    }


    public double calcAveragePrice(ArrayList<Double> monthlyPrice)
    {
        monthPriceAverage = 0;
        for(double billToken : monthlyPrice)
        {
            monthPriceAverage += billToken;
        }
        return monthPriceAverage / monthlyPrice.size();
    }


    public double calcElectricityCO2(double avgBill, double avgPrice)
    {
        emissionFactor = 1.37;
        months = 12;
        return annualCO2emission = avgBill / avgPrice * emissionFactor * months;
    }
}


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

        ArrayList<Double> monthlyBill = new ArrayList<Double>();                                                
        ArrayList<Double> monthlyPrice = new ArrayList<Double>();                                               
        double averageMonthlyBill,                                                                               
               averageMonthlyPrice,                                                                             
               annualCO2emission;                                                                               
        double monthlyBillToken1[] = {192.14, 210.42, 231.25, 186.13},                                           
               monthlyPriceToken1[] = {.07, .06, .08, .06};                                                      

        for(int index = 0; index < monthlyBillToken1.length; index++)                                            
        {
            monthlyBill.add(monthlyBillToken1[index]);                                                           
            monthlyPrice.add(monthlyPriceToken1[index]);                                                        
        }

        ArrayList<CO2FootprintV1> electricCO2outputput = new ArrayList<CO2FootprintV1>();


        averageMonthlyBill = CO2fromElectricity.calcAverageBill(monthlyBill);                                    
        averageMonthlyPrice = CO2fromElectricity.calcAveragePrice(monthlyPrice);                                 
        annualCO2emission = CO2fromElectricity.calcElectricityCO2(averageMonthlyBill, averageMonthlyPrice);      


        System.out.println("Average Monthly Electricity Bill: " + averageMonthlyBill);                           
        System.out.println("Average Monthly Electricity Prince: " + averageMonthlyPrice);                        
        System.out.println("Annual CO2 Emissions from Electricity Useage: " + annualCO2emission + "pounds");     
    }
}

Upvotes: 3

Views: 204

Answers (1)

Mark Elliot
Mark Elliot

Reputation: 77044

The issue is that calcAverageBill is a method that operates on objects of type CO2fromElectricity, but you're calling it as if it were a static method. That is, you must call it on an instance of CO2fromElectricity, e.g.:

CO2fromElectricity inst = new CO2fromElectricity();
averageMonthlyBill = inst.calcAverageBill(monthlyBill);

Alternatively, you can make your methods static by adding the static keyword to your method definitions:

public static double calcAverageBill(ArrayList<Double> monthlyBill)

Taking a quick look at CO2fromElectricity you probably want the fields you've defined (such as monthBillAverage to be local variables within each method rather than as fields or static properties, because your class doesn't benefit from defining these in the scope of an instance or in the scope of all executions (in fact, you might run into some trouble the way they're currently defined).

Upvotes: 3

Related Questions