Note that there are some explanatory texts on larger screens.

plurals
  1. POArray index error when duplicating a BufferedImage
    text
    copied!<p>So I'm trying to keep two copies of a set of images cached in my program. I've got an object that holds both references in it and a loop that loads the images and passes them to a new instance of that particular class. However, on about the 25th iteration, I get an Array Index Out of Bounds Exception when the class makes the copy. I've deduced that its trying to either copy to a non-existent pixel in the second copy or it's going beyond the bounds of the first copy.</p> <p>Here's the parts of the code that matter:</p> <p>The Loop:</p> <pre><code>Hashtable&lt;String, Sheet&gt; tempHashTable = new Hashtable&lt;String, Sheet&gt;(); for(int ii = 0; ii &lt; sheetNames.size(); ii++) { try { File tempSheetFile = new File(Constants.TEMP_DIR, sheetNames.get(ii)); BufferedImage tempSheet = ImageIO.read(tempSheetFile); System.out.println(sheetNames.get(ii)); tempHashTable.put(sheetNames.get(ii), new Sheet(tempSheet)); } catch (IOException ex) { } } </code></pre> <p>Sheet Init:</p> <pre><code>private BufferedImage defaultSheet; private BufferedImage currentSheet; public Sheet(BufferedImage defaultSheet) { this.defaultSheet = defaultSheet; currentSheet = new BufferedImage(defaultSheet.getWidth(), defaultSheet.getHeight(), BufferedImage.TYPE_INT_ARGB); currentSheet.setData(defaultSheet.getData()); //Error line } </code></pre> <p>Error:</p> <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 64 </code></pre> <p>The weirdest part about it is that the second BufferedImage is created with the size of the first, so why would we ever get an array index exception?</p> <p><strong>Edit</strong>: Alright, so I switched <code>BufferedImage.TYPE_INT_ARGB</code> to <code>defaultSheet.getType()</code> and the error's gone. The problem now is that the same images that were giving me the error before are now showing up inside the program without their red and alpha channels that definitely exist in the source files.</p> <p><strong>Edit</strong>: Never attribute to glitches what can be adequately explained by horrible programming. New code has the constructor load the image twice rather than copying an existing instance's data. Much cleaner, though I am using the <code>javapng</code> library available here <a href="http://code.google.com/p/javapng/" rel="nofollow">http://code.google.com/p/javapng/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload