0

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());
        
        
        
        
        
    }

}
 
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Didier L Feb 14 '22 at 12:01
  • Please also make sure to format the question properly as the main paragraphs are unreadable as they currently stand. – Didier L Feb 14 '22 at 12:02
  • You have an object reference (`this.mat`) that you believe points to an `int[][]` but it doesn't - it is just null. Go back through your initialization code to make sure that you have properly defined how that variable is instantiated and/or populated. – vsfDawg Feb 14 '22 at 17:16

1 Answers1

0

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];

puelo
  • 5,464
  • 2
  • 34
  • 62