This site requires JavaScript, please enable it in your browser!
Greenfoot back
DAustin15
DAustin15 wrote ...

2012/5/30

Error with movement code

DAustin15 DAustin15

2012/5/30

#
Hello Everyone, I was wondering if anyone could help me solve this issue I am having. When the board class compiles this is the error message I get:
java.lang.ArrayIndexOutOfBoundsException: -1
	at Board.<init>(Board.java:235)
	at sun.reflect.GeneratedConstructorAccessor6.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
	at greenfoot.core.Simulation.newInstance(Simulation.java:520)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:406)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:411)
	at greenfoot.core.Simulation.maybePause(Simulation.java:269)
	at greenfoot.core.Simulation.runContent(Simulation.java:201)
	at greenfoot.core.Simulation.run(Simulation.java:194)
There are 40 pads or game spots on the board which was the point of using 40 in the array. It seemed to compile when I put 39 in. Could anyone please help with this, I'm hoping to have the basics of my monopoly game finished by the end of my school year which is coming very soon.
 public Properties[] pads = new Properties[40];
    private Properties p;
    private int counter = 39; 
 

    /**
     * Constructor for objects of class Road.
     * 
     */
    public Board()
    {
        super(850,850,1);
        setBackground("Sociapoly.JPG");
        // This is currently unused, here to use as an example.
        //String bgTxt = "";
        //GreenfootImage img = new GreenfootImage(bgTxt, 30, Color.black, Color.magenta);
        //GreenfootImage bg = getBackground();
        //bg.drawImage(img, (bg.getWidth() - img.getWidth()) / 2, (bg.getHeight() - img.getHeight()) / 2);
        // The following statements sets up the Action menu items (in order given)        
        addObject(new Die1(), 337, 119);
        addObject(new Die2(), 399, 119);

        p= new Facebook();
        addObject(p, 554, 46);
        pads[counter] = p;
        counter--;

        p=new BrokenScreen();
        addObject(p, 499, 48);
        pads[counter] = p;
        counter--;

        p=new Twitter();
        addObject(p, 444 ,46);
        pads[counter] = p;
        counter--;

        p=new Ebay();
        addObject(p, 392 ,46 );
        pads[counter] = p;
        counter--;

        p=new Bing();
        addObject(p, 337 , 46 );
        pads[counter] = p;
        counter--;

        p=new YouTube();
        addObject(p, 283 , 46);
        pads[counter] = p;
        counter--;

        p=new Amazon();
        addObject(p, 228 , 46 );
        pads[counter] = p;
        counter--;

        p=new GooglePlus();
        addObject(p, 173 , 45 );
        pads[counter] = p;
        counter--;

        p=new ooVoo();
        addObject(p,118 ,46 );
        pads[counter] = p;
        counter--;

        p=new GoToJail();
        addObject(p,46 ,44 );
        pads[counter] = p;
        counter--;

        p=new Tumblr();
        addObject(p,46 ,113 );
        pads[counter] = p;
        counter--;

        p=new Norton();
        addObject(p,46 ,167 );
        pads[counter] = p;
        counter--;

        p=new Skype();
        addObject(p,47 ,217 );
        pads[counter] = p;
        counter--;

        p=new Skype();
        addObject(p,47 ,217 );
        pads[counter] = p;
        counter--;

        p=new Myspace();
        addObject(p,46 ,271 );
        pads[counter] = p;
        counter--;

        p=new Yahoo();
        addObject(p,44 ,325 );
        pads[counter] = p;
        counter--;

        p=new FourSquare();
        addObject(p,45 ,377 );
        pads[counter] = p;
        counter--;

        p=new Msn();
        addObject(p,45 ,429 );
        pads[counter] = p;
        counter--;

        p=new Ebay2();
        addObject(p,45 ,481 );
        pads[counter] = p;
        counter--;

        p=new Aim();
        addObject(p, 46 ,534 );
        pads[counter] = p;
        counter--;

        p=new FreeWifi();
        addObject(p,45,605 );
        pads[counter] = p;
        counter--;

        p=new LastFm();
        addObject(p,119  ,604 );
        pads[counter] = p;
        counter--;

        p=new Flickr();
        addObject(p,174  ,604 );
        pads[counter] = p;
        counter--;

        p=new Amazon3();
        addObject(p, 227 , 605 );
        pads[counter] = p;
        counter--;

        p=new Bebo();
        addObject(p, 280 , 605 );
        pads[counter] = p;
        counter--;

        p=new Google();
        addObject(p, 335, 603 );
        pads[counter] = p;
        counter--;
        
        p=new Yelp();
        addObject(p, 390,605);
        pads[counter] = p;
        counter--;
        
        p=new Gaia();
        addObject(p,445,605);
        pads[counter] = p;
        counter--;
        
        p=new Mcafee();
        addObject(p,498,606);
        pads[counter] = p;
        counter--;
        
        p=new GamerDNA();
        addObject(p,551,606);
        pads[counter] = p;
        counter--;
        
        p=new Jail();
        addObject(p,626,606);
        pads[counter] = p;
        counter--;
        
        p=new Ning();
        addObject(p,624,535);
        pads[counter] = p;
        counter--;
        
        p=new Wooxie();
        addObject(p,625,482);
        pads[counter] = p;
        counter--;
        
        p=new Ebay3();
        addObject(p,624,428);
        pads[counter] = p;
        counter--;
        
        p=new Habbo();
        addObject(p,625,376);
        pads[counter] = p;
        counter--;
        
        p=new Blekko();
        addObject(p,624,326);
        pads[counter] = p;
        counter--;
        
        p=new VirusDetected();
        addObject(p,624,272);
        pads[counter] = p;
        counter--;
        
        p=new Delicious();
        addObject(p,624,218);
        pads[counter] = p;
        counter--;
        
        p=new Amazon2();
        addObject(p,626,167);
        pads[counter] = p;
        counter--;
        
        p=new Zooppa();
        addObject(p,624,116);
        pads[counter] = p;
        counter--;
        
        p=new Go();
        addObject(p,625,46);
        pads[counter] = p;
        counter--;
        
        
davmac davmac

2012/5/30

#
You add 'Skype' twice. Hence, there are 41 objects added, counter gets decremented 40 times and reaches -1 before the last object is added; -1 is an invalid array index.
DAustin15 DAustin15

2012/5/30

#
Thank you very much, my group and I checked that and found the mistake a little while after we posted the message. We actually were able to get the loop working and the movement is working correctly now, thanks for the comment to help though.
You need to login to post a reply.