Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Index…
- User
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Index 73355 out of bounds for length 256. Fix this. // Гистограмма для sawtoottImage() private void sawtoothImage() { if (image != null) { float t1 = userInput("Введите значение порога 1: ", 0, 1); // Запрос значения первого порога float t2 = userInput("Введите значение порога 2: ", 0, 1); // Запрос значения второго порога
BufferedImage sawtoothContrastImage = sawtoothContrast(image, t1, t2); // Вызов функции пилообразного контрастирования с порогами setImageIcon(sawtoothContrastImage); saveImage(sawtoothContrastImage); // Создаем гистограмму исходного изображения int[] histogram = new int[256]; for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { int pixel = image.getRGB(i, j); int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = pixel & 0xff; int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue); histogram[brightness]++; } } int[] sawtoothHistogram = new int[256]; for (int i = 0; i < sawtoothContrastImage.getWidth(); i++) { for (int j = 0; j < sawtoothContrastImage.getHeight(); j++) { int pixel = sawtoothContrastImage.getRGB(i, j); int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = pixel & 0xff; int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue); if (brightness >= 0 && brightness < 256) { sawtoothHistogram[brightness]++; } } } - Assistant
The error is occurring because the array
sawtoothHistogramis initialized with a length of 256, but its indexes are being accessed up to 73355, which is out of bounds. To fix this, we need to make sure that the indexes are within the range of the array.One way to do this is to check if the brightness value is within the range of 0-255 before accessing the array. We can add a condition to the inner loop, like this:
for (int i = 0; i < sawtoothContrastImage.getWidth(); i++) { for (int j = 0; j < sawtoothContrastImage.getHeight(); j++) { int pixel = sawtoothContrastImage.getRGB(i, j); int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = pixel & 0xff; int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue); if (brightness >= 0 && brightness <= 255) { // Check if brightness is within the range sawtoothHistogram[brightness]++; } } }With this change, the program should no longer throw an
ArrayIndexOutOfBoundsException. - User
- Assistant
The error is occurring because the
sawtoothHistogramarray is being accessed out of bounds. To fix this, we can add a check to make sure that thesawtoothHistogramarray is not accessed out of bounds.Change the sawtoothHistogram loop to:
int[] sawtoothHistogram = new int[256]; for (int i = 0; i < sawtoothContrastImage.getWidth(); i++) { for (int j = 0; j < sawtoothContrastImage.getHeight(); j++) { int pixel = sawtoothContrastImage.getRGB(i, j); int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = pixel & 0xff; int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue); if (brightness >= 0 && brightness < 256) { sawtoothHistogram[brightness]++; // Check if brightness is within the range } } }With this change, the program should no longer throw an
ArrayIndexOutOfBoundsException.