关于#Indexoutofboundsforlength#的问题,如何解决?

在使用setJuzhen方法时提示错误,请问怎么解决

img


package shiyan2;
import java.util.Scanner;

public class Juzhen {
    private int row;
    private int col;
    private int a[][]=new int[row][col];
    
    public Juzhen() {
        
    }
    public Juzhen(int row,int col) {
        this.row=row;
        this.col=col;
    }
    public void setrow(int row) {
        this.row=row;
    }
    public void setcol(int col) {
        this.col=col;
    }
    public int getrow() {
        return row;
    }
    public int getcol() {
        return col;
    }
    public void setJuzhen() {
        Scanner s=new Scanner(System.in);
        int i=0,j=0;
        System.out.println("请输入矩阵的数据");
        for(i=0;i<row;i++) {
            for(j=0;jpublic void getJuzhen() {
        int i=0,j=0;
        for(i=0;i<row;i++) {
            for(j=0;j<row;j++) {
                System.out.print(a[i][j] );
            }
            System.out.println();
        }
    }
    public void ride(Juzhen m) {
        int i,j,k,p,q;
                int[][] x=new int[row][m.col];
                for(i=0;i<row;i++) {
                    for(j=0;jfor(k=0;kfor(p=0;p<row;p++) {
                for(q=0;qSystem.out.print(x[p][q] );
                }
                System.out.println();
            }
    }

}

a[][]=new int[row][col] 成员变量会在类实例化的时候初始化,而此时的rowcol都还是初始值0
改成下面这样

public class Juzhen {
    private int row = 0;
    private int col = 0;
    private int a[][] = null;

    public Juzhen() {

    }

    public Juzhen(int row, int col) {
        this.row = row;
        this.col = col;
        a = new int[row][col];
    }

    public void setrow(int row) {
        this.row = row;
    }

    public void setcol(int col) {
        this.col = col;
    }

    public int getrow() {
        return row;
    }

    public int getcol() {
        return col;
    }

    public void setJuzhen() {
        Scanner s = new Scanner(System.in);
        int i = 0, j = 0;
        System.out.println("请输入矩阵的数据");
        for (i = 0; i < row; i++) {
            for (j = 0; j < col; j++) {
                a[i][j] = s.nextInt();
            }
        }

    }

    public void getJuzhen() {
        int i = 0, j = 0;
        for (i = 0; i < row; i++) {
            for (j = 0; j < row; j++) {
                System.out.print(a[i][j]);
            }
            System.out.println();
        }
    }

    public void ride(Juzhen m) {
        int i, j, k, p, q;
        int[][] x = new int[row][m.col];
        for (i = 0; i < row; i++) {
            for (j = 0; j < col; j++) {
                for (k = 0; k < m.col; k++) {
                    x[i][k] += a[i][j] * m.a[j][k];
                }
            }
        }


        for (p = 0; p < row; p++) {
            for (q = 0; q < m.col; q++) {
                System.out.print(x[p][q]);
            }
            System.out.println();
        }
    }

}