Skip to content
Advertisement

Cannot read the array length because “” is null

I have an exercise in java whose text is:

The Matrix class represents an array of integers. Its skeleton is as follows: public class Matrix { private int [] [] mat; / * creates a Matrix object whose content is represented by the matrix matrix. * / public Matrix (int [] [] mat) {…} / * returns the sum of the elements of the matrix represented by the object on which the method is invoked. * / public int sumElements () {…} / * Returns the submatrix obtained from the one represented by the receiving object (this) by removing row i and column j * / public Submatrix matrix (int i, int j) {…} / * Returns a two-dimensional array of integers whose element (i, j) is the sum of the elements of the submatrix obtained from the receiving object (this) by removing row i and column j. * / public int [] [] subscriptValue () {…} / * Returns a textual representation of the array * / public String toString () {…}

Write the Matrix class and a Matrix Test class that contains only the main method and that performs the following actions:  Makes the user enter a two-dimensional array of integers a and creates a Matrix object m.  View the created object to the user.  Asks the user to enter an index of row i and an index of column j and displays the user the submatrix obtained from m by deleting row i and row j.  Show the user the values ​​of all the submatrixes obtained by removing for each possible pair (i, j) the row i and column j.

I don’t understand why the compiler gives me this error: “Exception in thread” main “java.lang.NullPointerException: Cannot read the array length because” this.mat “is null”

package Matrice1;

public class Matrice1 {
    private int[][] mat; 
    
    
    public Matrice1(int[][] m) {
        this.mat = new int[mat.length][mat[0].length];
        
        for(int i = 0; i < mat.length; i++) {
            for(int j = 0; j < mat[0].length; j++) {
                this.mat[i][j] = m[i][j]; 
            }
        }
    }
    
    
    public int sommaElementi() {
        int somma = 0; 
        
        for(int i = 0; i < this.mat.length; i++) {
            for(int j = 0; j < this.mat[0].length; j++) {
                somma += this.mat[i][j]; 
            }
            
        }
        return somma; 
    }
    
    
    public Matrice1 sottomatrice(int i, int j) {
        int [][] n = new int[this.mat.length -1][this.mat[0].length -1];
         
        for(int h = 0; h < n.length; h++) {
            for(int k = 0; k < n[h].length; k++) {
                int a = h; 
                int b = k; 
                if(a >= i) {
                    a++;
                }
                if(b >= j) {
                    b++;
                }
                n[h][k] = this.mat[a][b]; 
            }
        }
        Matrice1 m1 = new Matrice1(n);
        
        return m1; 
    }
    

    public int[][] valoreSottomatrici(){
        int[][] valore = new int[this.mat.length][this.mat[0].length]; 
        
        for(int i = 0; i < this.mat.length; i++) {
            for(int j = 0; j < this.mat[0].length; j++) {
                Matrice1 m = sottomatrice(i, j); 
                valore[i][j] = m.sommaElementi();
                
            }
        }
        return valore; 
    }
    
    public String toString() {
        String s = ""; 
        
        for(int i = 0; i < this.mat.length; i++) {
            for(int j = 0; j < this.mat[0].length; j++) {
                s += this.mat[i][j] + " ";
            }
            s += "n"; 
        }
        return s; 
    }

}


package Matrice1;

public class ProvaMatrice {
    public static void main(String[] args) {
        InputWindow in = new InputWindow(); 
        OutputWindow out = new OutputWindow(); 
        
        
        int k, h; 
        k = in.readInt("Numero righe: "); 
        h = in.readInt("Numero colonne: "); 
        int[][] m1 = new int[k][h]; 
        
        for(int i = 0; i < m1.length; i++) {
            for(int j = 0; j < m1[i].length; j++) {
                m1[i][j] = in.readInt("Inserisci valore in posizione " + i + " " + j +" : "); 
            }
        }
        
        Matrice1 mat = new Matrice1(m1); 
        
        out.write("Matrice inserita: " + mat.toString());
        
        
        
        
        
    }

}
 

Advertisement

Answer

You are using mat before it is initialized (probably a typo):

public Matrice1(int[][] m) {
        this.mat = new int[mat.length][mat[0].length];
        
        for(int i = 0; i < mat.length; i++) {
            for(int j = 0; j < mat[0].length; j++) {
                this.mat[i][j] = m[i][j]; 
            }
        }
    }

this.mat = new int[mat.length][mat[0].length]; should probably be this.mat = new int[m.length][m[0].length];

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement