0

I have an assignment to make a class based on another class called RBGColor which I don't have the source code for but I am told represents the color of a pixel like (0,255,0).

I have written the class but I have a null point exception on two of my methods.

  • getWidth
  • toGrayscaleArray

What interesting is that the method getHight doesn't throw that error.

   public class RGBImage{

   private RGBColor[][] _image;

    final RGBColor DEFAULT_COLOR_VALUE = new RGBColor(0,0,0);

    public RGBImage(int rows, int cols) {
        RGBColor[][] _image = new RGBColor[rows][cols];
    }

    public int getHeight(){
        int output = _image[0].length;
        return output;
    }

    public int getWidth(){
        int output = _image.length;
        return output;
    }

    public double[][] toGrayscaleArray(){
        double output[][] = new double[_image.length][_image[0].length];
        
        for (int i = 0; i < _image.length; i++){
            for (int j = 0; j < _image[0].length; j++){
                output[i][j] = _image[i][j].convertToGrayscale();// this is a method from the RGBColor class
            }
        }
        
        return output;
    }
   }
  • 1
    Your constructor does not initialise the field `_image`. It declares a local variable with the same name and initialises that. – khelwood May 01 '21 at 12:39
  • yeah you should remove the `RGBColor[][]` datatype in the constructor – Maurice May 01 '21 at 12:40

0 Answers0