# Why cant I call on the array I returned in the main method?

• November 16th, 2012, 02:07 PM
ColeTrain
Why cant I call on the array I returned in the main method?
Code java:

```//matrix1 double[][] matrix1 = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};   //matrix2 double[][] matrix2 = {{0, 2, 4},{1, 4.5, 2.2},{1.1, 4.3, 5.2}};   addMatrix(matrix1, matrix2);   printArrays(matrix1, matrix2, result, '+');     }//end of main   public static double[][] addMatrix(double[][] a, double[][] b){   double[][] result = {{a[0][0]+b[0][0], a[0][1]+b[0][1], a[0][2]+b[0][2]}, {a[1][0]+b[1][0], a[1][1]+b[1][1], a[1][2]+b[1][2]}, {a[2][0]+b[2][0], a[2][1]+b[2][1], a[2][2]+b[2][2]}};   return result; }   public static void printArrays(double[][] m1, double[][] m2, double[][] m3, char op) { for (int i = 0; i < m1.length; i++) { for (int j = 0; j < m1[0].length; j++) { System.out.print(" " + m1[i][j]); } if (i == m1.length / 2) System.out.print(" " + op + " "); else { System.out.print(" "); } for (int j = 0; j < m2[0].length; j++) { System.out.print(" " + m2[i][j]); } if (i == m1.length / 2) System.out.print(" = "); else { System.out.print(" "); } for (int j = 0; j < m3[0].length; j++) { System.out.print(" " + m3[i][j]); } System.out.println(); } } // public static void printArrays(double[][] a) { // for(int row = 0; row < a.length; row++){ // for (int col = 0; col < a[row].length; col++) { // // System.out.print(a[row][col] + " "); // }//end of inner for loop // System.out.println(""); // //}//end of outer for loop // }//end of printArrays   }//end of class```
• November 16th, 2012, 02:15 PM
copeg
Re: Why cant I call on the array I returned in the main method?
See the link in my signature entitled "Getting Help". I might be the only one, but have no clue what you are asking - the guidelines in that link will help you get your point/question across.
• November 16th, 2012, 03:01 PM
ColeTrain
Re: Why cant I call on the array I returned in the main method?
In my method addMatrix I return the array result. I then try to call it in my main method to print out, however it gives me an error saying it cannot find the variable. Since I returned it shouldnt I be able to call it?
• November 16th, 2012, 03:08 PM
copeg
Re: Why cant I call on the array I returned in the main method?
Quote:

Originally Posted by ColeTrain
In my method addMatrix I return the array result. I then try to call it in my main method to print out, however it gives me an error saying it cannot find the variable. Since I returned it shouldnt I be able to call it?

The method may have returned the matrix, but when you call the method you do not actually assign a reference to the returned value - the reference within the method being out of scope once the method returns. You must first assign a reference value to the returned value of the method before you can use it.