Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I got this error.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: rgb2 cannot be resolved to a variable

its always the rgb2 array that caused the error. How to solve this problem?

    BufferedImage img1 = ImageIO.read(file1);
    BufferedImage img2 = ImageIO.read(file2);

    int w = img1.getWidth();
    int h = img1.getHeight();

    long diff = 0;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        int rgb1[] = img1.getRGB(x, y, w, h, rgb1, 0, w);
        int rgb2[]= img2.getRGB(x, y, w, h, rgb2, 0, w);
        int index = y * w + x;
        int r1 = (rgb1[index] >> 16) & 0xff;
        int g1 = (rgb1[index] >>  8) & 0xff;
        int b1 = (rgb1[index]      ) & 0xff;
        int r2 = (rgb2[index] >> 16) & 0xff;
        int g2 = (rgb2[index]>>  8) & 0xff;
        int b2 = (rgb2[index]     ) & 0xff;
        r2 += Math.abs(r2 - r1);
        g2 += Math.abs(g2 - g1);
        b2 += Math.abs(b2 - b1);

        rgb2[index] = (((r2 & 0xff) << 16) + ((g2 & 0xff) << 8) +(b2 & 0xff));
        rgb2[index] = (rgb2[index]*17);
      }
    }

    int i = 0;
    for (int y = 0; y < h; y++) {
      int red = (y * 255) / (h - 1);
      for (int x = 0; x < w; x++) {
        int green = (x * 255) / (w - 1);
        int blue = 128;
        rgb2[i++] = (red << 16) | (green << 8) | blue;//the problem is at this line
      }
    }
    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    image.setRGB(0, 0, w, h, rgb2, 0, w);
    Graphics g = image.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    File imageFile = new File("saved.jpeg");
    ImageIO.write(image, "jpg", imageFile);

}

I got this error after declare outside. Exception in thread "main" java.lang.Error: Unresolved compilation problems: The local variable rgb1 may not have been initialized

    int w = img1.getWidth();
    int h = img1.getHeight();
    int scale = w * h * 3;
    int rgb1[] = img1.getRGB(0, 0, w, h, rgb1, 0, w);
    int rgb2[] = img2.getRGB(0, 0, w, h, rgb2, 0, w);
share|improve this question

2 Answers 2

Your problem is because rgb2[] is declared inside a for loop immediately before, in this line:

int rgb2[]= img2.getRGB(x, y, w, h, rgb2, 0, w);

Then the for loop ends, so rgb2[] falls out of scope and is released from memory, and no longer defined. If you want rgb2[] to be accessible from outside the loop, you have to say int rgb2[]; before the loop is called, so that the variable is in the same scope as the line where it needs to be called. Remember, scope is inherited downwards -- anything accessible immediately before the loop is accessible inside it -- but not the other way around.

share|improve this answer

You declared your rgb2 variable inside your first for loop, which is not visible to your second for loop, where the problem occurs. To fix it, simply declare rgb2 array before your for loops.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.