Ad
  • Default User Avatar
  • Default User Avatar
    public Matrix(double[][] elements) {   
      System.out.println("Entered the Constructor");
      try{
        print(elements);                      System.out.println("Passed Printing");
        matrix = clone(elements);             System.out.println("Passed Assignment");
      }catch(NullPointerException e){
        System.out.println("Error - Null \n" + "msg - " + e.getMessage() + "\n----------");
        throw new IllegalArgumentException();
      }catch(IndexOutOfBoundsException e){
        System.out.println("Error - Index\n" + "msg - " + e.getMessage() + "\n----------");
        //throw new RuntimeException();               // - Test Failed
        //throw new IndexOutOfBoundsException();      // - Test Failed
        //throw new NullPointerException();           // - Test Failed
        //throw new IllegalArgumentException();       // - should throw IllegalArgumentException if array is null: expected null, but was:<Matrix@2bbf180e>
        //throw new ArrayIndexOutOfBoundsException(); // - Test Failed
        // No Exception Thrown                        // - should throw IllegalArgumentException if array is null: expected null, but was:<Matrix@769e7ee8>
      }
      System.out.println("Reached Exit");
    }
    

    This is the Output Log

    Entered the Constructor
    Error - Null
    msg - null
    ----------

    Entered the Constructor
    size => rows = 2 cols = 2
    1.0 2.0
    Error - Null
    msg - null
    ----------

    Entered the Constructor
    size => rows = 2 cols = 3
    1.0 2.0 3.0
    1.0 2.0
    Passed Printing
    Error - Index
    msg - Index 2 out of bounds for length 2
    ----------

    Reached Exit

    The Error Message :-
    should throw IllegalArgumentException if array is null: expected null, but was:Matrix@769e7ee8

    Its always the same data. And the message is the above one or Test Failed. I tried different Exceptions but none worked. Also No Exception still doesn't works.
    Cannot use other than runtime Exceptions because the methods in the class creates the Matrix object by this constructor and then returns them. Like -> return new Matrix(someData);

    IndexOutOfBoundsException occurs due to the short length (2 rather than 3 as row 1) of 2nd row

    What to do.?

  • Default User Avatar
    public Matrix(double[][] elements) {   
      try{
        matrix = clone(elements); 
      }catch(NullPointerException e){
        throw new IllegalArgumentException();
      }
    }
    

    yup throwing the exception but it still wont work.
    clone(element) a function to copy all the elements to new array and return.

  • Default User Avatar

    I pass all the tests except one test1Init.

    It says:

    test1Init

    should throw IllegalArgumentException if array is null: expected null, but was:Matrix@14d3bc22


    can someone help.?

  • Default User Avatar

    Thanks! It worked!