Some friends and I created a rather large game replicating the agar.io game during our high school senior year in AP Comp Sci. The computers had Greenfoot 2.4.2 installed, so that's what we used. I am now trying to revive it as a little side project and perfect it, using the latest and greatest: Greenfoot 3.5.3. As far as I know, the Greenfoot.Color class was the only major change between the versions, and the game used it quite a bit but I've changed all the code accordingly. The problem occurs when I try to add an object to the world from an Actor class. I'm gonna focus on one thing, but it seems to happen with anything I add from the Actor class. During the World creation, I make a bunch of objects like this:
I call this to make 500 "proteins" at random places around the map. Works flawlessly. However, when my "Cell" actor runs into one and "eats" it, then Cell removes the offending "Protein" and then calls:
It crashes and returns:
I should also add that this game uses ScrollWorld and ScrollActors. Now I installed 2.4.2 on my computer, and using the same code it works perfectly fine, maybe even runs smoother. I've compared the World and Actor classes between the versions and they're basically the same. I don't know what I'm missing.
1 2 3 4 5 6 7 8 9 10 11 | public void spawnProteins( int amount) { for ( int i = 0 ; i < amount; i++) { int x = ( int )(Math.random() * (getFullWidth() - getWidth()) + (getWidth() / 2 )); int y = ( int )(Math.random() * (getFullHeight() - getHeight()) + (getHeight() / 2 )); int c = ( int )(Math.random() * 5 ); addObject( new Protein(c), x, y); ProteinPackage pPackage = new ProteinPackage(x, y, c); p.add(pPackage); } } |
1 2 3 4 5 | public void hitProtein() { removeTouching(Protein. class ); addMass( 1 ); ((Agar)getWorld()).spawnProteins( 1 ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | java.lang.NullPointerException at greenfoot.collision.ibsp.Rect.contains(Rect.java: 91 ) at greenfoot.collision.ibsp.IBSPColChecker.addObject(IBSPColChecker.java: 98 ) at greenfoot.collision.ColManager.addObject(ColManager.java: 123 ) at greenfoot.World.addObject(World.java: 438 ) at ScrollWorld.addObject(ScrollWorld.java: 160 ) //highlighted red at Agar.spawnProteins(Agar.java: 144 ) //highlighted red at Cell.hitProtein(Cell.java: 286 ) //highlighted red at Cell.act(Cell.java: 81 ) //highlighted red at greenfoot.core.Simulation.actActor(Simulation.java: 567 ) at greenfoot.core.Simulation.runOneLoop(Simulation.java: 530 ) at greenfoot.core.Simulation.runContent(Simulation.java: 193 ) at greenfoot.core.Simulation.run(Simulation.java: 183 ) |