# Thread: Difficulty with 2-d array :(

1. Member
Join Date
Feb 2012
Posts
14
Rep Power
0

## Difficulty with 2-d array :(

What i'm trying to do is grab each pixel of an image and store it in a 2-d array but when do some looping it always say out of index. My image is 240 in height and 364 in width. but my top loop only gives a number 0 and the second one works perfect and goes to its limits. For my code to work perfectly i need my top loop to go to 364 and both loops should work a total of 240*364 times. Hope you got my point.

Here's my code.

int wid = img.getWidth();
int hei = img.getHeight();

int[][] pixels = new int[hei][wid];

for(int i = 0; i<=wid; i++)
{
for(int j = 0;j<=hei;j++)
{

pixels[i][j] = img.getRGB(i,j);

int red = (((pixels[i][j] >> 16) & 0xff));
int green = (((pixels[i][j] >> 8) & 0xff));
int blue = ((pixels[i][j] & 0xff));
}

}

2. Moderator
Join Date
Apr 2009
Posts
12,873
Rep Power
22

## Re: Difficulty with 2-d array :(

i<=wid
and
j<=hei

Both of those should be '<'.
An array index goes up to array.length - 1.

Note that when posting code here you should use [code] tags [/code].
Also if your question involves and exception or error you should post the full error and stack trace, and point out the line on which it occurs in your code.

3. Member
Join Date
Feb 2012
Posts
14
Rep Power
0

## Re: Difficulty with 2-d array :(

still on this line ===> pixels[i][j] = img.getRGB(i,j);

I'm getting this error

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 240
at camera.JMFtest\$4.actionPerformed(JMFtest.java:131)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
at javax.swing.AbstractButton\$Handler.actionPerformed (AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.jav a:6505)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
at java.awt.Component.processEvent(Component.java:627 0)
at java.awt.Container.processEvent(Container.java:222 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
at java.awt.Component.dispatchEvent(Component.java:46 87)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713 )
at java.awt.Component.dispatchEvent(Component.java:46 87)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:707)
at java.awt.EventQueue.access\$000(EventQueue.java:101 )
at java.awt.EventQueue\$3.run(EventQueue.java:666)
at java.awt.EventQueue\$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain\$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain\$1.doIntersectionPri vilege(ProtectionDomain.java:87)
at java.awt.EventQueue\$4.run(EventQueue.java:680)
at java.awt.EventQueue\$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain\$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 677)

4. Moderator
Join Date
Apr 2009
Posts
12,873
Rep Power
22

## Re: Difficulty with 2-d array :(

Have you made the change I suggested?

5. Member
Join Date
Feb 2012
Posts
14
Rep Power
0

## Re: Difficulty with 2-d array :(

Yah i did it.. But still the same.

6. Member
Join Date
Feb 2012
Posts
14
Rep Power
0

## Re: Difficulty with 2-d array :(

I got it sorted out actually here ===> int[][] pixels = new int[hei][wid];
it should have been like this int[][] pixels = new int[wid][hei];

Thanks.. :)

7. Moderator
Join Date
Apr 2009
Posts
12,873
Rep Power
22

## Re: Difficulty with 2-d array :(

Well spotted.