I'm creating a 2D array program that accepts values from users and to perform two operations, such as Adding and Multiplying the array, then print the results. However, the program is not accepting any values, it prints the following error:
Exception in thread "main" java.lang.NullPointerException
at twodimensionalarray.Matrix.getMatrix(Matrix.java:2 7)
at twodimensionalarray.TwoDimensionalArray.main(TwoDi mensionalArray.java:18)
package twodimensionalarray; import java.util.Scanner; public class Matrix { double [][]Array; int rows; int cols; Matrix(){ rows = 0; cols = 0; int [][] Array = new int[rows][cols]; } Matrix(int row1, int col1) { rows = row1; cols = col1; } public void getMatrix(){ Scanner entry = new Scanner(System.in); System.out.println("Enter elements for array: "); for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ Array[i][j] = entry.nextDouble(); } } } public void displayMatrix(){ for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ System.out.printf("%2f " + Array[i][j]); } System.out.println(); } } public Matrix addMartix(Matrix first){ Matrix results = new Matrix(); if (rows != first.rows || cols != first.cols){ System.out.println("Unable to add matrix!"); }else{ for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ results.Array[i][j] = (int) (Array[i][j] + Array[i][j]); } } } return results; } public Matrix multiplyMartix(Matrix first){ Matrix results = new Matrix(); if (rows != first.rows || cols != first.cols){ System.out.println("Unable to multiply matrix!"); }else{ for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ results.Array[i][j] = (int) (Array[i][j] * Array[i][j]); } } } return results; } } package twodimensionalarray; import java.util.Scanner; public class TwoDimensionalArray { public static Matrix results; public static void main(String[] args) { int rows = 0, cols = 0; Scanner entry = new Scanner(System.in); System.out.println("Enter number of rows: "); rows = entry.nextInt(); System.out.println("Enter number of columns: "); cols = entry.nextInt(); Matrix a = new Matrix(rows, cols); a.getMatrix(); results = a.addMartix(a); results.displayMatrix(); results = a.multiplyMartix(a); results.displayMatrix(); } }