-5
import java.util.Scanner;
class TestMatrix
{
    public static void main(String args[]){
        Scanner in=new Scanner(System.in);
        System.out.println("Enter the number of matrices: ");
        int num=in.nextInt();
        int[][] temp=new int[10][10];
        Matrix[] matrixarray=new Matrix[num];
        Matrix.numberOfMatrices(num);
        for(int i=0;i<num;i++)
            {
                System.out.println("Enter the rows and columns of M["+(i+1)+"]: ");
                int r=in.nextInt();
                int c=in.nextInt();
                System.out.println("Enter the values: ");
                for(int x=0;x<r;x++)
                    for(int y=0;y<c;y++)
                        {
                            temp[x][y]=in.nextInt();
                        }
            matrixarray[i].inputMatrixValues(temp);     
        }
        }
}
public class Matrix
{
    static int number;
    int[][] matrix=new int[10][10];
    int row,col;
    public static void numberOfMatrices(int n)
    {number=n;}
    public void inputMatrixValues(int[][] matrix)
    {
        for(int i=0;i<row;i++)
            for(int j=0;j<col;j++)
            {
                this.matrix[i][j]=matrix[i][j];
            }
    }
}

the above code returns null pointer exception while calling the method inputMatrixValues() at line 22. matrixarray[i].inputMatrixValues(temp);

matrixarray is an object array of class Matrix. The exception occurs while accessing the ith element of object array. Matrix object array is created in line 9. Pls check what part of the code causes error.

AB BOIZ
  • 11
  • `Matrix[] matrixarray=new Matrix[num];` This creates an array of length num filled with null This is the cause of the exception because you're accessing one index of the array that only contains null references. – Yassin Hajaj Apr 01 '16 at 09:53
  • @Xoce웃Пepeúpa You're right, posted it as a comment instead. – Yassin Hajaj Apr 01 '16 at 09:54

1 Answers1

0

NullPointerException is raised because you have not initialized your Matrix[] matrixarray=new Matrix[num];. The matrixarray[0]{for example} is null and when you invoke matrixarray[i].inputMatrixValues(temp); this caused a NullPointerException

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85