-3

I have problem with my double arrays. After i run my File i got "null" values.

I do not know to parse this array above.

public class TestDesposit {
    public static void main(String[] args) {
        double [] rev = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0};
        double [] exp = {0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0};

        Result res = new Result (exp, rev);
        System.out.println(Arrays.toString(res.Resultat()));
    }
}

public class Result {
    double [] mExpenses;
    double [] mRevenue;
    double [] mResult;

    public Result (double[] pExpenses, double[] pRevenue) {
        mExpenses = pExpenses;
        mRevenue = pRevenue; 
    }

    public double [] Resultat () {
        for (int i = 0; i == 12; i++) {
            mResult[i] = mRevenue[i] - mExpenses[i];
        }
        return mResult;
    }   
}
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71

3 Answers3

1

You need to initialize your mResult array. Of course, this creates problems if your input arrays are not always going to have a length of 12, but that's a different problem.

You can try something like this:

double[] mResult = new double[12];

Then, as also people have been pointing out, you should change your for loop in Resultat to something like this:

for (int i = 0 ; i < mRevenue.length ; i++){

Note, that you can also use mResult.length and mExpenses.length as they will also be the same in your scenario.

Benjamin Lowry
  • 3,730
  • 1
  • 23
  • 27
0

At first, mResult is not init, so it is null object Second, this code

for (int i=0 ; i ==12; i++){.. }

never run , i think u should change like that

public double [] Resultat (){
   mResult  = new double[12];
        for (int i=0 ; i <12; i++){
        mResult[i] = mRevenue[i] - mExpenses[i];
        }
    return mResult;
    } 
Endymion
  • 43
  • 5
-1
for (int i=0 ; i ==12; i++){

is wrong. You should try i<12

tomas
  • 830
  • 3
  • 8
  • 25