-1

Matrix.java

    import java.io.*;
    class Matrix {
        private int q[][];

        public Matrix() {
            for(int i=0;i<3;i++)
            for(int j=0;j<3;j++)
                q[i][j] = Integer.parseInt(System.console().readLine());    
        }


        public Matrix( int a , int b ) {    
            int mat[][] = new int [a][b];
            for(int i=0; i<mat.length; i++) {
                for(int j=0;j<mat[i].length;j++)
                    q[i][j] = Integer.parseInt(System.console().readLine());
            }
        }



         public void show() {
            for(int i=0; i<q.length; i++) {
                for(int j=0;j<q[i].length;j++)
                    System.out.println(q[i][j]+" ");
            }   
        }
    }

UseMatrix.java

class UseMatrix {
    public static void main(String args[]) {

        Matrix m1 = new Matrix();  
        System.out.println("First Matrtix ");
        m1.show();

        Matrix m2 = new Matrix(5,4);
        System.out.println("Second Matrtix "); 
        m2.show();


    }
}

This programs shows NullPointerException error at runtime

Confused why this isn't working could use a little help, I want to the create a 2D Array of Size 3*3 through Default Constructors.

Then I want to create a Array of size 5*4 using parameterized constructors.

Akkië
  • 23
  • 1
  • 9
  • I can at least tell you that your for-loops needs {}-brackets to work. You have only defined brackets for 2 of your 6 loops. – jumps4fun Feb 05 '19 at 16:48
  • The brackets are okey. You can leave the brackets out but you have to remember only the following line will be looped, that's why is not encouraged. However the question did not had brackets issues. – corlaez Feb 05 '19 at 17:18

1 Answers1

0

There are a number of problems:

  • First: private int q[][]; // this holds a null value, never assigned on the parameterless constructor. A possible solution to this would be to add in your constructor: q[][] = new int[a][b]; and q[i] = new int[b];// each time you enter the loop. See the code below for clarity.

  • Second in public Matrix() { you try to access the array contents without checking its length (for(int i=0;i<3;i++) goes from 0 to 3, regarles of the actual array size). The array is empty at the beggining so this causes another NullPointerException here q[i][j] = Integer.parseInt... (I will solve this reusing the other constructor because they want to achieve the same result)

  • Third there is problems reading from console(), so I changed that too. (And added a line where you ask the user for a number)

  • And the last change I made was a small tweak to the show method.

Result:

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

public class App {
    public static void main(String[] args) {

        Matrix m1 = new Matrix();
        System.out.println("First Matrtix ");
        m1.show();

        Matrix m2 = new Matrix(5,4);
        System.out.println("Second Matrtix ");
        m2.show();
    }


}

class Matrix {
    private int q[][];
    private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public Matrix() {
        this(3, 3);
    }


    public Matrix(int a, int b ) {
        int value;
        System.out.println("Input a number to fill the new 3x3 matrix");
        try {
            value = Integer.parseInt(br.readLine());
        } catch (IOException e) {
            throw new RuntimeException("There was a problem reading the number from console", e);
        }
        q = new int[a][b];
        for(int i=0;i<a;i++) {
            q[i] = new int[b];
            for(int j=0;j<b;j++) {
                q[i][j] = value;
            }
        }
    }



    public void show() {
        for(int i=0; i<q.length; i++) {
            for(int j=0;j<q[i].length;j++)
                System.out.print(q[i][j]+" ");
            System.out.print("\n");
        }
    }
}

Program's output

corlaez
  • 1,002
  • 13
  • 28
  • so how can i correct this ? – Akkië Feb 05 '19 at 16:50
  • https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java – jumps4fun Feb 05 '19 at 16:52
  • 1
    but if i initialize the array then how i would use for size 3*3 using default constructor and later for 5*4 array using parameter constructor – Akkië Feb 05 '19 at 16:54
  • Basically your code is the equivalent of you saying that you have boxes to put something in, without actually having the boxes. Imagine you say to a blind person that you have a box, but when the blind perosn tries to put something in it, it just falls to the ground. Coding sometimes requires you to specify more than you think necessary. It takes a while to wrap your head around it. – jumps4fun Feb 05 '19 at 16:55
  • 1
    You can initialize it inside the constructor. And use the parameters sent to initialize it. Also, it is possible to re-initialize it, if you want it to reset. – jumps4fun Feb 05 '19 at 16:56
  • 1
    You are initializing it correctly in the parametered constructor btw. And because you use it inside the constructor, print it, and do not need to hold it, it would work to use a different variable. But you'd have to initialize the array inside the default constructor too, with the default [3][3] values – jumps4fun Feb 05 '19 at 17:00
  • plus, remember the brackets that you are missing. only two of your six for-loops have the brackets they need. { } – jumps4fun Feb 05 '19 at 17:01
  • The brackets are okey. You can leave the brackets out but you have to remember only the following line will be looped, that's why is not encouraged. However the question did not had brackets issues. – corlaez Feb 05 '19 at 17:13
  • You are right A.J. styles I didn't notice you want to initialize at constructor, but I made changes to my answer. – corlaez Feb 05 '19 at 17:19
  • @lrn2prgrm but I need to use only import java.io.*; only this package only – Akkië Feb 05 '19 at 18:01
  • Sure you can use `import java.io.*;``instead of the three import statements I wrote, it's all the same – corlaez Feb 05 '19 at 18:50