i have to build a 2D array that ask user to enter numbers to build the 2D array i know how to build this array using the for loop. But here i want to use the user input.
so anyone can help me ??
after i tried to run this program it display this error :
enter the elementss for the Matrix
Exception in thread "main" java.lang.NullPointerException
at test7.test7.fill(test7.java:29)
at test7.test7.main(test7.java:41)
this is my code:
package test7;
import java.util.Scanner;
public class test7 {
private int row = 4;
private int col = 4;
private int[][] matrix;
public test7(int trow, int tcol) {
this.row = trow;
this.col = tcol;
}
public test7(int trow, int tcol, int[][] m) {
this.row = trow;
this.col = tcol;
this.matrix = m;
}
public int[][] fill(){
System.out.println("enter the elementss for the Matrix");
Scanner in = new Scanner(System.in);
int[][] data = new int[0][0];
for(int i = 0; i< matrix.length; i++){
for(int j = 0 ;j< matrix[i].length; j++){
int x = in.nextInt();
}
}
return data;
}
public static void main(String[] args){
Question2 q2 = new Question2(3, 2);
q2.fill();
}
}