0

I'm trying to write matrices class in Java in order to create an encryption algorithm using multiplication of two matrices. I stuck on mult function that multiplies two input matrices. It always says there is an exception: java.lang.NullPointerException. I've spent an hour checking the codes and found nothing. Can anyone help me? Thanks in advance.

Exception in thread "main" java.lang.NullPointerException at Matrices.Matrices.printMatrix(Matrices.java:92) at Matrices.Matrices.main(Matrices.java:146)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Matrices
{
    public int[][] m;
    public Matrices()
    {

    }
    public Matrices(int[][] m)
    {
        this.m = m;
    }
    public static int[][] add(int[][] a, int[][] b)
    {
        if (a.length != b.length || a[0].length != b[0].length)
        {
            return null;
        }
        else
        {
            int[][] c = new int[a.length][a[0].length];
            int i,j;
            for (i = 0;i < a.length;i++)
            {
                for (j = 0;j < b.length;j++)
                {
                    c[i][j] = a[i][j] + b[i][j];
                }
            }
            return c;
        }
    }
    public static int[][] sub(int[][] a, int[][] b)
    {
        if (a.length != b.length || a[0].length != b[0].length)
        {
            return null;
        }
        else
        {
            int[][] c = new int[a.length][a[0].length];
            int i,j;
            for (i = 0;i < a.length;i++)
            {
                for (j = 0;j < b.length;j++)
                {
                    c[i][j] = a[i][j] - b[i][j];
                }
            }
            return c;
        }
    }
    public static int dotmult(int[] a,int[] b)
    {
        int c = 0;
        int i;
        for (i = 0;i < a.length;i++)
        {
            c = c + a[i] * b[i];
        }
        return c;
    }
    public static int[][] mult(int[][] a,int[][] b)
    {
        if (a.length != b[0].length)
        {
            return null;
        }
        else
        {
            int[][] c = new int[a[0].length][b.length];
            int i,j,k;
            for (i = 0;i < a[0].length;i++)
            {
                for (j = 0;j < b.length;j++)
                {
                    for (k = 0; k < b[0].length; k++)
                        c[i][j] += a[i][k] * b[k][j];
                }
            }
            return c;
        }
    }
    public void printMatrix(int[][] a)
    {
        int i,j;
        for (i = 0;i < a[0].length;i++)
        {
            for (j = 0;j < a.length;j++)
            {
                System.out.print(a[i][j] + " ");
            }
            System.out.println("");
        }
    }
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        /*System.out.print("Type the number of rows of Matrix a: ");
        int m1 = Integer.parseInt(br.readLine());
        System.out.print("Type the number of columns of Matrix a: ");
        int m2 = Integer.parseInt(br.readLine());
        int[][] a = new int[m1][m2];*/
        int[][] a = new int[3][2];
        a[0][0] = 1;
        a[0][1] = 0;
        a[1][0] = 0;
        a[1][1] = 1;
        a[2][0] = 1;
        a[2][1] = 0;

        /*System.out.print("Type the number of rows of Matrix b: ");
        int n1 = Integer.parseInt(br.readLine());
        System.out.print("Type the number of columns of Matrix b: ");
        int n2 = Integer.parseInt(br.readLine());
        int[][] b = new int[n1][n2];*/
        int[][] b = new int[2][2];
        b[0][0] = 2;
        b[0][1] = 3;
        b[1][0] = 1;
        b[1][1] = 2;
        int i,j;
        /*System.out.print("Type the elements of Matrices a: ");
        for (i = 0;i < m1;i++)
        {
            for (j = 0;j < m2;j++)
            {
                a[i][j] = Integer.parseInt(br.readLine());
            }
        }
        System.out.print("Type the elements of Matrices b: ");
        for (i = 0;i < n1;i++)
        {
            for (j = 0;j < n2;j++)
            {
                b[i][j] = Integer.parseInt(br.readLine());
            }
        }*/
        int[][] c = mult(a,b);
        Matrices m = new Matrices(c);
        m.printMatrix(c);
    }
}
  • may be need to change a.length to a[0].length in for (i = 0;i < a[0].length;i++) issue is in second for loop with j variable { for (j = 0;j < a.length;j++) { System.out.print(a[i][j] + " "); } System.out.println(""); – sanjeevRm Mar 23 '21 at 10:38
  • See [java - Matrix multiplication using arrays - Stack Overflow](https://stackoverflow.com/questions/17623876/matrix-multiplication-using-arrays) –  Mar 23 '21 at 10:47

1 Answers1

0

You should review your application constraints and how you are multiplying your matrices:

public static int[][] mult(int[][] a,int[][] b)
{
    if (a.length != b[0].length) // you are always falling into this path since a.lenght == 3 while b[0].lenght == 2
    {
        return null;
    }
    else
    {
        // do multiplication
    }
}

hence the error you are receiving when printing the result matrix c which is obviously null since there has been no multiplication.

tmarwen
  • 15,750
  • 5
  • 43
  • 62