# Problem Calculating emmisions

• August 6th, 2013, 11:19 AM
llowe29
Problem Calculating emmisions
I get NaN for all the values when I run the code. Not sure why? Here is the code.
Code Java:

```import java.util.ArrayList; class CO2FromElectricity   { CO2FromElectricity() { //default constructor to be used }   public double calcAverageBill(ArrayList<Double> monthlyBill) { // **** insert code for method here ****// monthlyBill = new ArrayList<Double>(); double aveBill = 0; double bIS = 0; for(int i = 0; i < monthlyBill.size(); i++) bIS += monthlyBill.get(i); aveBill = bIS / monthlyBill.size(); return aveBill; }   public double calcAveragePrice (ArrayList<Double> monthlyPrice) { // **** insert code for method here ****// monthlyPrice = new ArrayList<Double>(); double avePrice = 0; double pS = 0; for(int i = 0; i < monthlyPrice.size(); i++) pS += monthlyPrice.get(i); avePrice = pS / monthlyPrice.size(); return avePrice; }   public double calcElectricityCO2 (double avgBill, double avgPrice) { // **** insert code for method here ****// double emmisions = ((avgBill / avgPrice)*1.37*12); return emmisions; } }```
And for the tester.
Code Java:

```import java.util.ArrayList; public class CO2FromElectricityTester { public static void main(String[] args) { CO2FromElectricity CO2 = new CO2FromElectricity();   ArrayList<Double> monthlyBill = new ArrayList<Double>(); ArrayList<Double> monthlyPrice = new ArrayList<Double>();   //Values to add to the monthly bill or use your own: monthlyBill.add(209.60); monthlyBill.add(249.68); monthlyBill.add(222.59); monthlyBill.add(209.60); monthlyBill.add(249.68); monthlyBill.add(222.59); monthlyBill.add(209.60); monthlyBill.add(249.68); monthlyBill.add(222.59); monthlyBill.add(209.60); monthlyBill.add(249.68); monthlyBill.add(222.59);   // Values to add to the monthly Price or use your own: //   monthlyPrice.add(209.70 / 2464); monthlyPrice.add(249.68 / 2948); monthlyPrice.add(222.59 / 2621); monthlyPrice.add(209.70 / 2464); monthlyPrice.add(249.68 / 2948); monthlyPrice.add(222.59 / 2621); monthlyPrice.add(209.70 / 2464); monthlyPrice.add(249.68 / 2948); monthlyPrice.add(222.59 / 2621); monthlyPrice.add(209.70 / 2464); monthlyPrice.add(249.68 / 2948); monthlyPrice.add(222.59 / 2621);   double avgBill = CO2.calcAverageBill(monthlyBill); double avgPrice = CO2.calcAveragePrice(monthlyPrice);   double emissions = CO2.calcElectricityCO2(avgBill, avgPrice);   System.out.printf("Average Monthly Electricity Bill: %6.2f%n", avgBill); System.out.printf("Average Monthly Electricity Price: %4.2f%n", avgPrice); System.out.printf("Annual CO2 Emissions from Electricity Usage: %7.1f pounds", emissions); } }```
• August 6th, 2013, 11:26 AM
KevinWorkman
Re: Problem Calculating emmisions
I recommend stepping through this with a debugger, or at least adding some print statements, to figure out what's going on with your code. Hint: make sure you use {} after if statements and for loops!
• August 6th, 2013, 11:26 AM
GregBrannon
Re: Problem Calculating emmisions
You're not using the values passed to the CO2 methods, but creating empty/null values to use instead:
Code :

``` public double calcAverageBill(ArrayList<Double> monthlyBill) { // **** insert code for method here ****// monthlyBill = new ArrayList<Double>(); double aveBill = 0; double bIS = 0; for(int i = 0; i < monthlyBill.size(); i++) bIS += monthlyBill.get(i); aveBill = bIS / monthlyBill.size(); return aveBill; }```
If you delete or comment out the line,

monthlyBill = new ArrayList<Double>();

all should be well. Do you understand why? If not, please ask.
• August 6th, 2013, 11:27 AM
llowe29
Re: Problem Calculating emmisions
Wait why?
Also should I do the same thing for Monthly Price.
• August 6th, 2013, 11:30 AM
GregBrannon
Re: Problem Calculating emmisions
If you understood why, you wouldn't have to ask, but try it and see what happens. Then figure out why. If you can't, ask.

Oh, wait a minute. You did ask.

In the method calcAverageBill(), the ArrayList<Double> of values is passed as an argument called 'monthlyBill.' That's what the method's signature means:

public double calcAverageBill(ArrayList<Double> monthlyBill)

Then, the following statement declares and initializes a new LOCAL variable ArrayList<Double> of null values:

monthlyBill = new ArrayList<Double>();

replacing the values passed to the method. From there, all calculations done by the method use the monthlyBill ArrayList that is essentially empty (though initialized with some number of null values.)

Does that make sense? And, yes, you need to do the same with the calcAveragePrice() method.
• August 6th, 2013, 11:38 AM
llowe29
Re: Problem Calculating emmisions
I realized it worked but don't understand why the value would be null.

--- Update ---

Oh now I see it's because its already initialized in param, so when you reinitialize in the subcode itself it is null, I suppose , because Java is sequential, it takes the null value. Thanks.
• August 6th, 2013, 11:45 AM
GregBrannon
Re: Problem Calculating emmisions
Maybe you get it, but I can't tell from your choice of words. I'm not sure about the sequential part.

I would say it differently: The values passed from the main() method to the CO2FromElectricity methods in the argument variables were replaced by new local variables of the same name (that's why they are replaced) that are initialized empty/null.