0

I'm not very good at programming, but I am obliged to write a program that calculates the product of two matrices by two methods :

The first method in a direct way.

The second using Threads so that the computation time is minimal.

I wrote this program for the first method :

Matrix.java

 public class Matrix   {
    public int [][] M;
    public  int line,col;
    static int  [][]MProd=null;

    //Constructeur

    public Matrix (int [][] M,int line,int col) {
        this.M=M;
        this.col=col;
        this.line=line;
        for ( int i=0;i<this.line;i++){
         for (int j=0;j<this.col;j++)
            {
                M[i][j]=(int)(Math.random()*100);}}
     }

     static int [][] prod(Matrix Mat1, Matrix Mat2 ){

            for (int j=0;j<Mat2.col;j++){
                 for (int i=0;i<Mat1.col;i++){
                    for (int k=0;k<Mat1.line;k++){
                         MProd[k][j] += Mat1.M[k][i]*Mat2.M[i][j];
                    }}}
        return MProd ;  

      }}

Main.java

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
        int [][] M = null,N = null;
        int line1,line2,col1,col2;
        int [][] P=null;
        Scanner scanner = new Scanner(System.in);

        System.out.println("enter the line number of the first matrix");
        line1=scanner.nextInt();

        System.out.println("enter the number of columns of the first matrix");
        col1=scanner.nextInt();
        Matrix Mat= new Matrix (M,line1,col1);

        System.out.println("enter the line number of the 2nd matrix");
        line2=scanner.nextInt();

        System.out.println("enter the number of columns of the 2nd matrix");
        col2=scanner.nextInt();

        Matrix Mat1= new Matrix (N,line2,col2);


           if (col1==line2)
        {
        P=Matrix.prod(Mat,Mat1) ; 

        System.out.println("matrix product :");
        for (int i=0;i<Mat.line;i++)
            {
     for (int j=0;j<Mat1.col; j++)
            System.out.print( + P[i][j]+" ");
            System.out.println();
            }}

        else {
         System.out.println("the matrices product is impossible");
        }
        }}

when I run the program it shows me :

Exception in thread "main" java.lang.NullPointerException at Matrice.(Matrice.java:18) at Main.main(Main.java:15)

Can someone help me to correct this program, and shows me how to write this program with Threads ?

Med
  • 111
  • 5
  • 4
    You've posted before, and so you know the drill: 1) Please specify what you mean by "does not work". 2) Please ask specific questions as your second question, "...how to write this program with Threads" is way too broad, and really isn't an answerable question. You must ask about what *specifically* confuses you, since surely you've read up on the subject and have some ideas. – Hovercraft Full Of Eels Dec 14 '13 at 12:23
  • @HovercraftFullOfEels this is the first time I post here :) – Med Dec 14 '13 at 12:26
  • 3
    Ah, your score fooled me. Then sorry for being blunt, but still, I think that it will serve you and us well if you work to improve your question. Again, "does not work" gives us little information with which we can use to help you. Again, your second part of the question is way too broad. We do much better answering specific pointed questions after showing code attempts. – Hovercraft Full Of Eels Dec 14 '13 at 12:27
  • @HovercraftFullOfEels Now this is good ? – Med Dec 14 '13 at 12:32
  • Which line is line 18 of the Matrice.java class, as noted in `Matrice.java:18`? And not yet, you still have yet to ask a specific question or questions regarding your second part. We have no idea what problems you may be having with it. – Hovercraft Full Of Eels Dec 14 '13 at 12:32
  • @HovercraftFullOfEels here M[i][j]=(int)(Math.random()*100); – Med Dec 14 '13 at 12:35
  • possible duplicate of [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – Raedwald Feb 28 '14 at 13:16

1 Answers1

1

Your NullPointerException is due to this:

public static void main(String[] args) {
        int [][] M = null,N = null;  // all declared null

        // ...

        Matrix Mat= new Matrix (M,line1,col1); // here you use a null value, M

        // ... 

        Matrix Mat1= new Matrix (N,line2,col2);  // and same here, N is null

When you call, M[i][j]=(int)(Math.random()*100); in your Matrix class, M is null so this will fail. You must first create a Matrix, your 2D array, before you can use it.


Edit
You ask:

Can you explain me more.

You're passing a null reference into your Matrix constructor and then trying to use it as a variable. You should pass a non-null 2D array instead:

int[][] myArray = new int[x][y]; // where x and y are appropriate numbers
Matrix Mat= new Matrix (myArray, line1, col1);

Also, please study and use standard Java Coding Conventions (please see link) including:

  • All class, enum and interface names begin with an uppercase letter.
  • All method and variable names begin with a lowercase letter.
  • Learn to use judicious use of white space including a space between parameters.

Do this and folks will be better able to read and understand your code and then be able to give you better help.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373