Hey guys, I'm having an issue for the following sn...
# getting-started
c
Hey guys, I'm having an issue for the following snippets. One crashes, the other doesn't crash.
Copy code
val bufferedImgFile: BufferedImage = ImageIO.read(File(filePath))
val imageMatrix: Array<IntArray> = Array(height) { IntArray(width) }
(0..(bufferedImgFile.height - 1)).map { hgt ->
        (0..(bufferedImgFile.width - 1)).map { wdt ->
          imageMatrix[hgt][wdt] = bufferedImgFile.getRGB(hgt, wdt)
        }
      }
And
Copy code
val bufferedImgFile: BufferedImage = ImageIO.read(File(filePath))
val imageMatrix: Array<IntArray> = Array(height) { IntArray(width) }
async {
        (0..(bufferedImgFile.height - 1)).map { hgt ->
          async {
            (0..(bufferedImgFile.width - 1)).map { wdt ->
              data[hgt][wdt]= bufferedImgFile.getRGB(hgt, wdt)
            }
          }
        }
      }
The former would crash with an
ArrayOutOfBoundsException
, the later doesn't crash and completes. Please why is this so?
a
Where does the
async
function come from? Anko? Coroutines?
c
Coroutines
a
Ok, so the reason you don't get an exception here is that
async
catches Exceptions and puts it into the returned
Deferred
if you use
async  { ... }.await()
the same exception should be thrown
c
Aha! Thanks for that. Please one more thing. The dimensions of the buffered image is 960 by 828, from the definition for
BufferedImage.getRGB
it says the
An ArrayOutOfBoundsException may be thrown if the coordinates are not in bounds
GIven the dimensions, am I supposed to be getting this error?
a
@chi the function is defined as
getRGB(x: Int, y: Int)
, so it expects the horizontal coordinate as the first parameter, you flipped them
c
Gracias for the observation!
👍 1
p
so I'm still learning... where does data come from in the second snippet? data[hgt][wdt]=bufferedImgFile.getRGB(hgt,wdt)
c
@philipharder, I made a mistake there, it was supposed to be
imageMatrix[hgt][wdt]= bufferedImgFile.getRGB(hgt, wdt)