修改方法内的全局变量

时间:2016-10-12 19:04:50

标签: java

我是新来的,所以请耐心等待。

所以,我有一个全局变量

private Pixel[][] result;

有RGB

public Pixel(int r, int g, int b){
    this.r=r;
    this.g=g;
    this.b=b;
}

方法modifyBrightness()

public void modifyBrightness(String arg0) {
    result = new Pixel[height][width];
    try {
        int brightness;
        brightness= Integer.parseInt(arg0);

        Pixel[][] brillo = new Pixel[height][width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int r = ValidateRGB(original[i][j].getR() + brightness);
                int g = ValidateRGB(original[i][j].getG() + brightness);
                int b = ValidateRGB(original[i][j].getB() + brightness);
                result[i][j] = new Pixel(r, g, b);


            }
        }                   
    }

到目前为止,代码工作正常。但是,如果我尝试从不同的方法访问结果,我仍然会得到NullPointer异常

public void saveResult(String arg0) {
    BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
    for (int i = 0; i < imagen.length; i++) {
        for (int j = 0; j < imagen.length; j++) {
            int rgb = ((resultado[i][j].getR() & 0x0ff) << 16) | ((resultado[i][j].getG() & 0x0ff) << 8)
                    | (resultado[i][j].getB() & 0x0ff);
            result.setRGB(i, j, rgb);
        }
    }
}

1 个答案:

答案 0 :(得分:-2)

确保在result [i] [j]处有一个元素。您必须在数组中放置一个元素。