Monday, September 1, 2014

Issue with For Loop




I'm trying to make a test program, but I appear to have encountered an error, I think with a for loop.



My goal is to make an 8x8 area of squares be randomly generated as black or white, although my eventual goal is more complex.



I have a "Block" class, which has 3 variables in it (I forget the name for that sort of variable, you know, in a class...):



isSolid, which keeps track of whether the block is black or white.



X and Y coordinates, which keep track of where the block is in a grid.



I create a list of blocks, depending on the size x and y size of the "world" given to the generator.



I then give x and y values to these blocks, based on the spot in both of two for loops, as well as which block it is on: the x for loop, which only loops once the y loop has finished one iteration (nested for loops). Apparently, I have some issue within the y for loop; but I can't figure out exactly how to fix it. I think maybe the for loop is going to far, but I can't figure out where.




Here's my code. I don't know if there are [code] tags for this forum, but I can do spoilers.



The Block class:






Java Code:






public class Block {
boolean isSolid = false;
int xCoord;
int yCoord;
}




The GameWorld class:





Java Code:







import java.util.Random;

public class GameWorld {
int worldSizeX;
int worldSizeY;

Block[] worldBlockList;

public GameWorld(int x, int y) {
//Assign Stuff
worldSizeX = x;
worldSizeY = y;

worldBlockList = new Block[x * y];

//Set up each Block in the list to remember its X and Y coordinates
int totalProgress = 0;
int xProgress;
int yProgress;

for (xProgress = 0; xProgress < worldSizeX; xProgress++) {
for (yProgress = 0; yProgress < worldSizeY - 1; yProgress++) {
//TODO - Fix this. The issue is NOT with x/yProgress, probably
worldBlockList[totalProgress].xCoord = xProgress;
worldBlockList[totalProgress].yCoord = yProgress;

totalProgress++;
}
}
}

public void genWorld() {
//TODO - SUPER TEMPORARY WORLDGEN
Random random = new Random();
for (int i = 0; i > (worldSizeX * worldSizeY); i++) {
worldBlockList[i].isSolid = random.nextBoolean();
}
}

//I might not need this
public void wipeWorld() {
for (int i = 0; i > (worldSizeX * worldSizeY); i++) {
worldBlockList[i].isSolid = false;
}
}
}




My error report (the issue seems to be with line 24):

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException



at GameWorld.<init>(GameWorld.java:24)



at GamePanel.<init>(GamePanel.java:10)



at Run.initUI(Run.java:17)



at Run.<init>(Run.java:10)



at Run$1.run(Run.java:31)



at java.awt.event.InvocationEvent.dispatch(Unknown Source)



at java.awt.EventQueue.dispatchEventImpl(Unknown Source)



at java.awt.EventQueue.access$200(Unknown Source)



at java.awt.EventQueue$3.run(Unknown Source)



at java.awt.EventQueue$3.run(Unknown Source)



at java.security.AccessController.doPrivileged(Native Method)



at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)



at java.awt.EventQueue.dispatchEvent(Unknown Source)



at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)



at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)



at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)



at java.awt.EventDispatchThread.pumpEvents(Unknown Source)



at java.awt.EventDispatchThread.pumpEvents(Unknown Source)



at java.awt.EventDispatchThread.run(Unknown Source)




I hope that helped, and I hope you can help me. :)




Hmm... I hope those spoilers worked. On an unrelated note, could someone tell me what are used as spoiler tags on these forums?




EDIT: Rats, the formatting appears to have gotten borked. How am I supposed to post code here?



EDITEDIT: Figured out code tags. No need to explain now. :P







No comments:

Post a Comment