Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree2Likes

Thread: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

  1. #1
    Join Date
    Apr 2012
    Posts
    90
    Rep Power
    0

    Question Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    The following is the code that working but given error in cousole----
    ERROR is ----
    java.lang.ArrayIndexOutOfBoundsException: 52
    java.lang.ArrayIndexOutOfBoundsException: 53
    java.lang.ArrayIndexOutOfBoundsException: 54
    java.lang.ArrayIndexOutOfBoundsException: 55
    java.lang.ArrayIndexOutOfBoundsException: 56
    java.lang.ArrayIndexOutOfBoundsException: 57
    .
    .
    .
    etc.
    And also problem in, When I am dragging one card of 52 bunch and move over applet Is applet blink continue when over the card on the applet, and nothing is showing when I open next tab, You can check this ERROR on the following URL:-

    Progresso-Not The Soup

    Please help me to find out ERROR.
    Help ASAP.

    My Code is:
    ShowCard .java
    Java Code:
    package com.progresso;
    
    import java.applet.AudioClip;
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    
    import javax.swing.JApplet;
    
    public class ShowCard extends JApplet implements MouseListener, 
    MouseMotionListener {
    
    	
    	private static final long serialVersionUID = 1L;
    	Image image,img,Casino_table_img;
    	MediaTracker tr;
        int x;
        int y;
      	private static final int IMAGE_WIDTH = 73;
        private static final int IMAGE_HEIGHT = 97;
        //--- instance variables
        /** Initial image coords. */
        private int _initX     = 0;   // x coord - set from drag
        private int _initY     = 0;   // y coord - set from drag
        
        /** Position in image of mouse press to make dragging look better. */
        private int _dragFromX = 0;  // Displacement inside image of mouse press.
        private int _dragFromY = 0;
        
        private ShowCard[] _deck = new ShowCard[52];
        private ShowCard _currentCard = null;  // Current draggable card.
        Button play,stop;
        AudioClip audioClip;
                   String suits = "shdc";
            String faces = "a23456789tjqk";
            int cardPosition = 0;
        
            public ShowCard()
            {
            	Demo1(image,x,y);
            }
        public ShowCard(Image image, int x, int y) {
        	this.image = image;
            this.x = x;
            this.y = y;
            //System.out.println("Card is: "+this.image);
    			// TODO Auto-generated constructor stub
    		}
    	public void Demo1(Image image, int x, int y)  //here x and y is 0-51;
        {
            this.image = image;
            this.x = x;
            this.y = y;
            //System.out.println("Card is: "+this.image); // image name of the package cards.cards
              //-- Initialize graphics
                //setPreferredSize(new Dimension(600, 600));
                setBackground(Color.gray);
                setForeground(Color.BLACK);
                //--- Add mouse listeners.
                
             	
        }
    	
    	public void init()
    	{
    		audioClip = getAudioClip(getCodeBase(), "audio/TestSnd.wav");
    		this.addMouseListener(this); 
            this.addMouseMotionListener(this);
    	}
        public void paint(Graphics g) {
        	
            super.paintComponents(g);   // Required
            //-- Display the cards, starting with the first array element.
            //   The array order defines the z-axis depth.
            try {                
                //image = ImageIO.read(new File("cards/2c.gif"));
             
            for (int suit=0; suit<suits.length(); suit++) 
            {
                for (int face=0; face<faces.length(); face++) 
                {
                	
                    //ImageIcon img = new ImageIcon("cards/"+ faces.charAt(face)
                                                 //+ suits.charAt(suit) + ".gif");
                	tr = new MediaTracker(this);
                	  img = getImage(getCodeBase(),"cards/"+ faces.charAt(face)
                              + suits.charAt(suit) + ".gif");
                	  Casino_table_img = getImage(getCodeBase(),"cards/casino_table2.gif");
                	  tr.addImage(img,0);
                	  //g.drawImage(img, 0, 0, this);
                    //image = ImageIO.read(new File("cards/"+ faces.charAt(face)
                          //  + suits.charAt(suit) + ".gif"));// all card fetch from here in loop
                    _deck[cardPosition++] = new ShowCard(img, _initX++, _initY++);
                    //System.out.println("1");
                   
                }
            } 
            }catch (Exception ex) {
            	System.out.println(ex);
                // handle exception...
           }
            int crd=0;
           
            g.drawImage(Casino_table_img, 20, 20, this);
            for(int i=0;i<10;i++)
            {
            for (crd=0; crd<_deck.length; crd++) {
            	 ShowCard c = _deck[crd];
                
                //c.image.paintIcon(this, g, c.x, c.y);// here c.x=0-51 and c.y=0-51
                //g.drawOval(150, 150, 100, 100);
                g.drawImage(c.image, c.x,c.y, this);
                //System.out.println("OK");
               	
    			} 
            
            }
               //g.drawString("Progresso Application", 100, 50);
                
               
        
        }//end paintComponent
        
      //===================================================== method mousePressed
        /** When the mousePressed listener is called,
        check if it's in the area of a card..  If it is, 
        <ol>
        <li>_currentCard is set so mouseDragged knows what to drag.</li>
        <li>Record where in the image (relative to the upper left coordinates) 
            the mouse was clicked, because it looks best if we drag from there.</li>
        </ol>
        TODO: Move the card to the last position so that it displays on top.
        */
       
    	public void mousePressed(MouseEvent e) {
            int x = e.getX();   // Save the x coord of the click
            int y = e.getY();   // Save the y coord of the click
            
            //-- Find card image this is in.  Check from top down.
            _currentCard = null;  // Assume not in any image.
            for (int crd=_deck.length-1; crd>=0; crd--) {
            	ShowCard testCard = _deck[crd];
                if (x >= testCard.x && x <= (testCard.x + IMAGE_WIDTH)
                        && y >= testCard.y && y <= (testCard.y + IMAGE_HEIGHT)) {
                    _dragFromX = x - testCard.x;  // how far from left
                    _dragFromY = y - testCard.y;  // how far from top
                    _currentCard = testCard;  // Remember what we're dragging.
                    System.out.println(_currentCard);
                   
                    audioClip.play();
                  
                    break;   // Stop when we find the first match.
                }
            }
        }//end mousePressed
        
     
        //============================================================ mouseDragged
        /** Set x,y to mouse position and repaint. */
        public void mouseDragged(MouseEvent e) {
            if (_currentCard != null) {   // Non-null if pressed inside card image.
                _currentCard.x = e.getX() - _dragFromX;
                _currentCard.y = e.getY() - _dragFromY;
                
                //--- Don't move the image off the screen sides
                _currentCard.x = Math.max(_currentCard.x, 0);
                _currentCard.x = Math.min(_currentCard.x, getWidth()-IMAGE_WIDTH);
                
                //--- Don't move the image off top or bottom
                _currentCard.y = Math.max(_currentCard.y, 0);
                _currentCard.y = Math.min(_currentCard.y, getHeight()-IMAGE_HEIGHT);
                
                this.repaint(); // Repaint because position changed.
            }
        }//end mouseDragged
    
        //====================================================== method mouseExited
        /** Turn off dragging if mouse exits panel. */
        public void mouseExited(MouseEvent e) {
            _currentCard = null;
            System.out.println("7");
        }//end mouseExited
    
        //=============================================== Ignore other mouse events.
        public void mouseMoved   (MouseEvent e) {  }  // ignore these events
        public void mouseEntered (MouseEvent e) {  }  // ignore these events
        public void mouseClicked (MouseEvent e) {  }  // ignore these events
        public void mouseReleased(MouseEvent e) {  }  // ignore these events
    	
    	
    }
    Help:-
    Last edited by Norm; 04-13-2012 at 12:49 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,424
    Rep Power
    25

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    What lines are the errors occuring on?
    Your problem is that the index to an array is past the end of the array. Change the code to keep the array within the size of the array.

    Add a call to the printStackTrace() method to all the catch blocks to get the full text of the error messages which will show what lines the errors occurred on.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Apr 2012
    Posts
    90
    Rep Power
    0

    Question Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    Quote Originally Posted by Norm View Post
    What lines are the errors occuring on?
    Your problem is that the index to an array is past the end of the array. Change the code to keep the array within the size of the array.

    Add a call to the printStackTrace() method to all the catch blocks to get the full text of the error messages which will show what lines the errors occurred on.
    I have used printStackTrace() method in Catch block , now error is-----

    java.lang.NullPointerException
    at com.progresso.ShowCard.paint(ShowCard.java:116)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:734)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:679)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Re paintManager.java:659)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(SystemEventQueueUtilities.java:128)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)

    Hense the problem should be in the line 116,
    Line is - _deck[cardPosition++] = new ShowCard(img, _initX++, _initY++);
    I have tried but not solving properly, please help me where I am wrong.
    Last edited by ganesh.gothi@gmail.com; 04-13-2012 at 10:02 AM. Reason: Add line that occur Error

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    Stop what you're doing and systematically go through some tutorials first. I just glanced at your code and I see a paint(...) override that calls super.paintComponents(...) with a comment // Required. Then there appears to be a lot of resource loading and media tracking in the painting method. That won't do. a painting method must be lean and fast, must not perform application logic and must not get bogged down in I/O.

    Also, the line you quote is line 98 in the code you posted. Are you sure it's line 116 in the original code?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,424
    Rep Power
    25

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    What variable has a null value on the line where the exception occurred?
    If you can not see by looking at the code, add a println just before the line with the error and print out the values of all the variables used on that line.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Join Date
    Apr 2012
    Posts
    90
    Rep Power
    0

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    Quote Originally Posted by Norm View Post
    What variable has a null value on the line where the exception occurred?
    If you can not see by looking at the code, add a println just before the line with the error and print out the values of all the variables used on that line.
    The line you quote is line 98 in the code I have posted.
    Now The error on the same line occur when I comment the following code that you have suggest.

    // super.paintComponents(g); // Required

    but again occur the same error.

    Error on code as bellow
    _deck[cardPosition++] = new ShowCard(img, _initX++, _initY++); after this I have as that you have suggest is--

    System.out.println(_deck[cardPosition++]);
    Out print on console is NULL
    And When I have commented this ERROR code no cards is display on the Applet.
    What can I do now.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,424
    Rep Power
    25

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    What variable has a null value? Is it the _deck variable?

    Where do you assign values to it? What is the value of cardPosition when the NPE occurs?
    Did you assign values to all the elements in the array?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Join Date
    Apr 2012
    Posts
    90
    Rep Power
    0

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    _deck[cardPosition++] variable has a null value but its working and show all 52 cards but give exception on console
    java.lang.ArrayIndexOutOfBoundsException:51,
    java.lang.ArrayIndexOutOfBoundsException:52....... ..

    here _deck[cardPosition++] = new ShowCard(img, _initX++, _initY++);
    I have created object is _deck[cardPosition++] and here
    and _deck is a array of 52.
    anything else.....

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,424
    Rep Power
    25

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    java.lang.ArrayIndexOutOfBoundsException:51
    That says that the array does not have 52 slots in it.
    Remember that the indexes to arrays go from 0 to the array length-1

    What is the value of cardPosition when you print out: _deck[cardPosition] and it prints a null value?
    Note: the ++ was removed for the debug code to not change the variable's values that could effect the real code
    Last edited by Norm; 04-13-2012 at 02:06 PM.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Join Date
    Apr 2012
    Posts
    90
    Rep Power
    0

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    _deck[cardPosition++] = new ShowCard(img, _initX++, _initY++);
    _deck[cardPosition] gives null value when I am printing this.
    System.out.println(cardPosition);


    Its gives o/p in console

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    java.lang.ArrayIndexOutOfBoundsException: 52
    at com.progresso.ShowCard.paint(ShowCard.java:95)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:734)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:679)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Re paintManager.java:659)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(SystemEventQueueUtilities.java:128)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)
    java.lang.ArrayIndexOutOfBoundsException: 53
    at com.progresso.ShowCard.paint(ShowCard.java:95)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:734)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:679)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Re paintManager.java:659)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(SystemEventQueueUtilities.java:128)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,424
    Rep Power
    25

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    The array does not have an element at index=52
    Remember that the indexes to arrays go from 0 to the array length-1
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Join Date
    Apr 2012
    Posts
    90
    Rep Power
    0

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    here I have print this System.out.println(cardPosition);
    Its give output 1-52 Its shows loop is running 52 times.

    This is not the array element I have printed Here also My array is going to 0-51 is right.
    where I can change in my code that remove the exception,

    Thanks for the spend time with me.......

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,424
    Rep Power
    25

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    where I can change in my code that remove the exception,
    Do not use an index value greater than the array length-1. Stop incrementing the index value when it is pointing to the last element in the array. Check your logic to see why the program continues incrementing the index when the index is at the end of the array.

    Have you made the corrections recommended in post #4?

    Stop doing anything else until you have made those changes.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Join Date
    Apr 2012
    Posts
    90
    Rep Power
    0

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    I have only commented the super.paintComponents but no effect, I will try,,,,,,,
    I have one question more My applet is blinking when I am dragging card and also back card is dragged is always show back side of the above card in the bunch, please check the link that I have made, Please solve my this more problem , Link code is the same code that I have posted in the first post(#1),
    Please check tthe following link and give reply ASAP that what can I do for solve this problem. Link is
    Progresso-Not The Soup

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,424
    Rep Power
    25

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    Sorry, I do not go to private links. Post anything you want comments on here on the forum.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Join Date
    Apr 2012
    Posts
    90
    Rep Power
    0

    Question Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    Quote Originally Posted by Norm View Post
    Sorry, I do not go to private links. Post anything you want comments on here on the forum.
    This is my private link, so you can check, if not, ok no problem.
    I am explaning my problem
    In the #1 code when I am dragging one card , my hole applet is blinking continue when I am moving this drag card, any code problem as dragging card or I will extend JPannel in the programe or any problem, please if you dont mind, Please check link at once, Its only for this that you can easily understand my problem, please visit once, drage card and move arround the screen, Please help me I am trying this problem for three day's.

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    Quote Originally Posted by ganesh.gothi@gmail.com View Post
    Please check tthe following link and give reply ASAP that what can I do for solve this problem.
    Quote Originally Posted by ganesh.gothi@gmail.com View Post
    Please check link at once.
    Please check this link.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,424
    Rep Power
    25

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    Until you fix the code as recommended in post #4 testing could be a waste of time.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    Join Date
    Apr 2012
    Posts
    90
    Rep Power
    0

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    [QUOTE=Norm;278831]Until you fix the code as recommended in post #4 testing could be a waste of time.[/QUOT E]

    Please give me a link for the "tutorials" as #4 posts you was said.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,424
    Rep Power
    25

    Default Re: Problem in code Generate java.lang.ArrayIndexOutOfBoundsException.

    Here is a link:
    The Really Big Index
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. java.lang.ArrayIndexOutOfBoundsException: 20
    By xminiguyx in forum New To Java
    Replies: 1
    Last Post: 12-14-2011, 04:54 PM
  2. Replies: 3
    Last Post: 11-16-2011, 01:32 AM
  3. java.lang.ArrayIndexOutOfBoundsException: 6
    By pyat77 in forum Java Applets
    Replies: 6
    Last Post: 03-06-2010, 01:12 PM
  4. java.lang.ArrayIndexOutOfBoundsException
    By mensa in forum Java 2D
    Replies: 7
    Last Post: 05-05-2008, 09:09 AM
  5. java.lang.ArrayIndexOutOfBoundsException
    By riccian in forum New To Java
    Replies: 0
    Last Post: 03-18-2008, 09:38 AM

Tags for this Thread

Posting Permissions

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