Reputation: 237
I am currently trying to get my head wrapped around imagej, and just trying to duplicate an image and spit out the output. The general idea is to get the image, get the pixel array off the image processor, copy it into a new array, and build up a new image from that array.
Now in this, the value of aPixels_org[0] exists, as you can see by the console output. but its still throwing an arrayoutofbounds exception, even with all my checks.
Here is the code... (btw l. is just an logger object, and just a sanity check to check if the array values exist in the if condition below. Do advise if there is a better way, as i am a c++ guy, and am used to checking stuff directly, as java wont allow the if(int){} check)
I am getting the aPixels_org from a function, an dthe aPixels_dup is just created as such
int[] aPixels_dup = new int[iWidth*iHeight];
where iWidth*iHeight is the size of the array needed. I have also tried to generate the size directly with the array.length() option. The problem code is
System.out.println(">>>>>>>>>>>>>>>>>>>>>." + aPixels_dup[0] +" " + aPixels_org[0] + " " + iWidth + " " + iHeight);
if(Integer.toString(aPixels_org[0]) != null && Integer.toString(aPixels_dup[0]) != null )
{
l.info("Arrays exsist");
System.arraycopy(aPixels_org,0,aPixels_dup,0,iWidth*iHeight);
l.info("Array Copied");
}
else{l.warn("Something is NULL");}
l.info("");
This is the error
java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at Alpha_.DuplicateCurrentImage(Alpha_.java:64)
at Alpha_.run(Alpha_.java:81)
at ij.IJ.runUserPlugIn(IJ.java:185)
at ij.IJ.runPlugIn(IJ.java:152)
at ij.Executer.runCommand(Executer.java:127)
at ij.Executer.run(Executer.java:64)
at java.lang.Thread.run(Thread.java:679)
Here is the console output
[INFO] % FILE: Alpha_.java % CLASS: Alpha_ % FUNCTION: DuplicateCurrentImage % LINE: 59 % COMMENT: OK % % % TIMESTAMP: 2013-01-17 00:26:55,942
0 -1393202 466 466
[INFO] % FILE: Alpha_.java % CLASS: Alpha_ % FUNCTION: DuplicateCurrentImage % LINE: 63 % COMMENT: Arrays exsist % % % TIMESTAMP: 2013-01-17 00:26:55,942
Thats the end of my output, and the program crashes with the above error. As you can see, all the 4 values exist in some form or the other. I googled the error and found that the error is
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
Am i missing something obvious here?
RE
Upvotes: 0
Views: 1358
Reputation: 234847
Both arrays—aPixels_org
and aPixels_dup
—must have length at least iWidth*iHeight
(which is 217156, according to your console output). One (or both) of the arrays must be shorter that that, which is why you are getting the exception. Also be sure that the value of iWidth*iHeight
did not increase between the allocation of aPixels_dup
and the call to System.arraycopy
.
It would be safer to use Math.min(aPixels_org.length, aPixels_dup.length)
as the last argument to System.arraycopy
. However, that would hide any length errors in your code. You might want to assert
that both arrays have the expected length.
Also, your checks seem to be meaningless. The fact that an element at index 0 exists in both arrays is kind of irrelevant. Also, Integer.toString(int)
never returns null
. Here's a version of your code that does some sensible checks:
if (aPixels_org != null &&
aPixels_dup != null &&
aPixels_org.length == aPixels_dup.length &&
aPixels_org.length == iWidth*iHeight )
{
l.info("Array sizes match");
System.arraycopy(aPixels_org,0,aPixels_dup,0,iWidth*iHeight);
l.info("Array Copied");
}
else{l.warn("Something is NULL or dimensions are off");}
l.info("");
Upvotes: 2
Reputation: 37823
It's all in JavaDoc. I highlighted the reason I think is the cause for your error.
public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)
...
Otherwise, if any of the following is true, an IndexOutOfBoundsException
is thrown and the destination is not modified:
srcPos
argument is negative.destPos
argument is negative.length
argument is negative.srcPos+length
is greater than src.length
, the length of the source array.destPos+length
is greater than dest.length
, the length of the destination array.Upvotes: 0