I have an assignment to make a class RGBImage based on another class called RBGColor which I don't have the source code (and don't need to) for but I am told it represents the color of a pixel like (0,255,0) and I was given the methods of that class.
I have written the class but I have a null point exception on one of my methods.
Some background code:
public class RGBImage{
private RGBColor[][] _image;
final RGBColor DEFAULT_COLOR_VALUE = new RGBColor(0,0,0);
public RGBImage(int rows, int cols) {
_image = new RGBColor[rows][cols];
}
The problematic method:
public RGBColor[][] toRGBColorArray(){
RGBColor[][] output = new RGBColor[_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].setRed(_image[i][j].getRed());// NPE here.
output[i][j].setGreen(_image[i][j].getGreen());
output[i][j].setBlue(_image[i][j].getBlue());
}
}
return output;
}
I have used setRed and getRed in other methods without problem so far so I don't understand why it's problematic now.