×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    5

    ArrayIndexOutOfBoundsException

    hey in my code im processing 2 imgs....both around 200x200....while processing d first one i dont ve ne prob......but wen aplyin d same steps on other img its giving ArrayIndexOutOfBoundsException....

    Code:
    byte[][] R = new byte[200][200];
     byte[][] s = new byte[200][200];
     double m1[] = new double[FP_TEMPLATE_MAX_SIZE];
     double m2[] = new double[FP_TEMPLATE_MAX_SIZE];
     		
     R = grayScale(ip);
     R = ThinningHilditch(R,ip);
     R = ThinningHitAndMiss(R,ip);
     m1 = getFingerPrintTemplate(R,ip);
     printFeatures(m1);
        	
     s = grayScale(ip2);
     s = ThinningHilditch(s,ip2);
     s = ThinningHitAndMiss(s,ip2);
     m2 = getFingerPrintTemplate(s,ip2);
    the grayScale function is i think d source of problem

    Code:
    public byte[][] grayScale(Image img)
    	{
    		
    		byte P[][] = new byte[200][200];
    		int red1,green1,blue1,cl,clr,clg,clb;
    	
    		//int[]rgbData = new int[img.getWidth() * img.getHeight()]; //array containin rgb of each pixel
            img.getRGB(rgbData, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight()); //rgb of each pixel goin in the array
            
        	int [][]rgbData_two_d = new int [img.getWidth()][img.getHeight()];
    		
    		for(i1 = 0; i1 < img.getWidth(); i1++)
            {
            	
            	for(j1= 0; j1 <img.getHeight(); j1++)
                	{
                    	
                    	rgbData_two_d[i1][j1] = rgbData[temp];
                        temp++;
                    }       
                    
                    System.out.println("");
       		}
     
          	for(i1 = 0; i1 < img.getWidth(); i1++)
          	{
        		for(j1= 0; j1 <img.getHeight(); j1++)
                { 
                	cl= rgbData_two_d[i1][j1];
               		clr= rgbData_two_d[i1][j1];
                   	clb= rgbData_two_d[i1][j1];
                  	clg= rgbData_two_d[i1][j1];
                           	
                   	clr= clr & 0x00ff0000;
                 	red1= clr>>16;
                  	clg= clg & 0x0000ff00;
               		green1= clg>>8;
                   	clb= clb & 0x000000ff;
              		blue1= clb;
                           	
                   	if ((blue1<= 127) && (red1<= 127) && (green1 <= 127))        
                  	{
                       	P[i1][j1] = 1;
                  	}
                           	
               		else
                  	{
               			P[i1][j1] = 0;
                	}
                       
           		}
                        
         	}
                   
          	for( i1 = 0; i1<= img.getWidth() - 1;i1++)
    		{
      			P[i1][0] = 0;
      			P[i1][img.getHeight()- 1] = 0;
      		}
      			  
      		for( j1 = 0 ; j1<= img.getHeight() - 1 ; j1++)
      		{
       			P[0][j1] = 0;
         		P[ img.getWidth() - 1][j1] = 0;
        	
    		}
    		
    		return P;	
    	}
    here is d stack trace

    java.lang.ArrayIndexOutOfBoundsException
    java.lang.ArrayIndexOutOfBoundsException
    at LoginMIDlet.grayScale(+99)
    at LoginMIDlet.allFun(+87)
    at LoginMIDlet.startApp(+12)
    at javax.microedition.midlet.MIDletProxy.startApp(+7)
    at com.sun.midp.midlet.Scheduler.schedule(+270)
    at com.sun.midp.main.Main.runLocalClass(+28)
    at com.sun.midp.main.Main.main(+116)

    plzzzzz help!!!!

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: ArrayIndexOutOfBoundsException

    I think it is because you do not initialize the variable "temp".

    You use lots of non-local variables, and this will cause you endless problems. Declare your variables where you use them, in the smallest possible scope.

    You call getWidth() and getHeight() often. The return the same values each time, so it is much more efficient to call them once, and store the values.

    Also, there is no need to re-organize the data into a 2D array, then convert to bytes. You can do both as one step. Otherwise, you could be using a very large amount of memory.

    I've simplified the code a little. Try to keep your code as simple as you can, and it will be easier for you to find problems.

    PHP Code:
    public byte[][] grayScale(Image img) {
        
    int width img.getWidth();
        
    int height img.getHeight();

        
    int[] rgbData = new int[width height];
        
    img.getRGB(rgbData0width00widthheight);

        
    byte[][] = new byte[width][height];

        for (
    int x 0widthx++) {
            for (
    int y 0heighty++) {
                
    int p rgbData[(width) + height];
                
    int alpha = (>> 24) & 0xff;
                
    int red   = (>> 16) & 0xff;
                
    int green = (>>  8) & 0xff;
                
    int blue  =  p        0xff;

                if ((
    blue <= 127) && (red <= 127) && (green <= 127)) {
                    
    P[x][y] = 1;
                } else {
                    
    P[x][y] = 0;
                }
            }
        }

        for (
    int x 0widthx++) {
            
    P[x][0] = 0;
            
    P[x][height 1] = 0;
        }

        for (
    int y heighty++) {
            
    P[0][y] = 0;
            
    P[width 1][y] = 0;
        }

        return 
    P;    


  3. #3
    Super Contributor
    Join Date
    Jan 2008
    Location
    Amravati, India
    Posts
    546

    Re: ArrayIndexOutOfBoundsException

    You can store 8 bits in 1 byte. So better do it like this:
    Code:
    byte[][] P = new byte[(width + 7) >>> 3][(height + 7) >>> 3];

Similar Threads

  1. ArrayIndexOutOfBoundsException in RecordComparator
    By kaseychen in forum Mobile Java General
    Replies: 2
    Last Post: 2009-05-23, 19:28
  2. ArrayIndexOutOfBoundsException on drawPolygon method
    By MobileVisuals in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2006-01-10, 17:33
  3. DirectGraphics.drawPixels() giving ArrayIndexOutOfBoundsException on 6600
    By mac1ej in forum Mobile Java Media (Graphics & Sounds)
    Replies: 5
    Last Post: 2004-04-21, 10:09
  4. ArrayIndexOutofBoundsException
    By rapix in forum Mobile Java General
    Replies: 2
    Last Post: 2003-08-11, 21:07
  5. ArrayIndexOutOfBoundsException
    By hertsi in forum Mobile Java General
    Replies: 4
    Last Post: 2003-03-21, 20:48

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×