java - Exception in main thread. Do not know the reason -
package examples; import java.util.scanner; public class matrixmultiplication { public static void main(string[] args) {
the below 4 sections identifies user input rows , columns of 2 matrices.
scanner userrows1 = new scanner(system.in); system.out.println("enter number of rows matrix 1: "); int rows1 = userrows1.nextint(); scanner usercolumns1 = new scanner(system.in); system.out.println("enter number of columns matrix 2"); int columns1 = usercolumns1.nextint(); scanner userrows2 = new scanner(system.in); system.out.println("enter number of rows matrix 2: "); int rows2 = userrows2.nextint(); scanner usercolumns2 = new scanner(system.in); system.out.println("enter number of columns matrix 2"); int columns2 = usercolumns2.nextint();
this sets objects matrix1 , matrix2 belonging class matrix
matrix matrix1 = new matrix(rows1, columns1); matrix matrix2 = new matrix(rows2, columns2); matrix1.showmatrix(); system.out.println("\n \n"); matrix2.showmatrix(); } } class matrix { int rows; int columns; int[][] values; public matrix(int r, int c) { rows = r; columns = c; int[][] values = new int[r][c];
this served allow user input values of matrix 1 one. set values of matrix value simplicity.
int i; int j; for(i = 0; < r; i++) { for(j = 0; j < c; j++) { //scanner userelement = new scanner(system.in); //system.out.println("enter number:"); //int element = userelement.nextint(); values[i][j] = 1; } } } public void showmatrix() { int k; int l; for(k = 0; k < rows; k++) { for(l = 0; l < columns; l++) { system.out.println(values[k][l] + " "); } system.out.println("\n"); } } } code above. in final method in class matrix (the method showmatrix), trying print out matrix. however, using general values matrix here , says: exception in thread "main" java.lang.nullpointerexception @ examples.matrix.showmatrix(matrixmultiplication.java:75) @ examples.matrixmultiplication.main(matrixmultiplication.java:29) can diagnose issue? i'm still new java.
you've not instantiate field [][]values
(there local declaration of int[][] values
).
public matrix(int r, int c) { rows = r; columns = c; int[][] values = new int[r][c]; <-- remove values = new int[r][c]; .... }
Comments
Post a Comment