首页 > 解决方案 > 对象类方法的空点异常

问题描述

我有一个任务是根据另一个名为RBGColor的类创建一个类,我没有源代码,但我被告知代表像素的颜色,如 (0,255,0)。

我已经编写了这个类,但是我的两个方法有一个空点异常。

有趣的是 getHight 方法不会抛出该错误。

   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;
    }
   }

标签: javanullpointerexception

解决方案


推荐阅读