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

2014/7/17

What is causing this code to stop?

MusicPenguin MusicPenguin

2014/7/17

#
1 public void setValuesAndImages(){ 2 for (int x = 0; x < 4; x++){ 3 for (int y = 0; y < 4; y++){ 4 Tile tile = (Tile) getObjectsAt(x, y, Tile.class).get(0); 5 if (tile != null && tile.getDidCombine()){ 6 tile.setValueOfTile(); 7 tile.setIndividualImage(tile.getValueOfTile()); 8 tile.setDidCombine(); 9 } 10 } 11 } 12 } sorry about not using the code tags. my browser isn't getting along with me. once it reaches line 5 it stops. The method call of tile.getDidCombine() is just recalling a boolean variable.
CooliMC CooliMC

2014/7/17

#
can you post the class tile and more code so that i can see how to fix that ?
danpost danpost

2014/7/17

#
If it is stopping on line 5, then you must be getting some type of exception message. You need to post that message along with its stack trace. BTW, you can insert the code tags around your code snippets yourself if needed. Before the first line of your code, insert the word 'code' surrounded by square brackets; and at the end of your code, insert '/code' surrounded by square brackets.
NikZ NikZ

2014/7/17

#
Clicking the "5" on the very left of your window should turn the "5" into a stop sign. If that happened, your code will stop there. .
MusicPenguin MusicPenguin

2014/7/17

#
I was using the greenfoot version that runs from a usb on a windows xp vm so I think that interfered with the console coming up with an error. So in short I was thoroughly confused ha. But since I'm here, here is the code.
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(ArrayList.java:635)
	at java.util.ArrayList.get(ArrayList.java:411)
	at TileBoard.setValuesAndImages(TileBoard.java:107)
	at TileBoard.canCombine(TileBoard.java:86)
	at TileBoard.setKeyDirection(TileBoard.java:48)
	at TileBoard.checkKeys(TileBoard.java:36)
	at Tile.act(Tile.java:28)
	at greenfoot.core.Simulation.actActor(Simulation.java:568)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
104 public void setValuesAndImages(){
105        for (int x = 0; x < 4; x++){
106            for (int y = 0; y < 4; y++){
107               Tile tile = (Tile) getObjectsAt(x, y, Tile.class).get(0);
108                if (tile != null && tile.getDidCombine()){
109                    tile.setValueOfTile();
110                    tile.setIndividualImage(tile.getValueOfTile());
111                    tile.setDidCombine();
112                }
113            }
114        }
    }
let me know what you think
danpost danpost

2014/7/17

#
Line 4 is the problem line; not line 5. You need to make sure that 'getObjectsAt' will not return an empty list before using 'get' on it. Insert between lines 4 and 5 above the following line:
if (getObjectsAt(x, y, Tile.class).isEmpty()) continue;
The will cause execution to move on to the next iteration of the for loops without processing any more code within the block on the current iteration if there is no tile at the current location being checked.
MusicPenguin MusicPenguin

2014/7/18

#
Ahhhhhh yes! Thank you very much!
You need to login to post a reply.