Results 1 to 3 of 3
  1. #1
    type1ninja is offline Member
    Join Date
    May 2014
    Location
    In orbit above Kerbin
    Posts
    2
    Rep Power
    0

    Question 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
    Last edited by type1ninja; 09-02-2014 at 01:59 AM. Reason: Tried to add code tags

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

    Default Re: Issue with For Loop

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at GameWorld.<init>(GameWorld.java:24)
    There was a null value on line 24 that caused the exception. Look at the code on line 24 and find where the null value was. Then backtrack to see where the null value came from.

    Note on creating arrays of objects. There are two steps:
    1) change the array of slots with initial empty/null values
    2) assign references to objects to the empty slots
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    type1ninja is offline Member
    Join Date
    May 2014
    Location
    In orbit above Kerbin
    Posts
    2
    Rep Power
    0

    Default Re: Issue with For Loop

    Quote Originally Posted by Norm View Post
    There was a null value on line 24 that caused the exception. Look at the code on line 24 and find where the null value was. Then backtrack to see where the null value came from.

    Note on creating arrays of objects. There are two steps:
    1) change the array of slots with initial empty/null values
    2) assign references to objects to the empty slots
    Sorry, I should have been more clear: I DO see that one of my values is null, and I see that it is on line 24, I suppose I was hoping you could help me find the null thing. Having slept on it, I think it may be the actually object in the list of blocks, and not any of the values in the loop. I'll try changing the x and y coordinate assignment to
    Java Code:
    worldBlockList[totalProgress] = new Block(false, xProgress, yProgress);
    instead of what it was before. Thanks for the help. :)

Similar Threads

  1. While loop issue.
    By kylekep8 in forum New To Java
    Replies: 2
    Last Post: 10-05-2012, 08:58 AM
  2. issue with loop or IDE
    By neoginn in forum New To Java
    Replies: 3
    Last Post: 10-28-2011, 06:59 AM
  3. Loop Issue
    By burrish in forum New To Java
    Replies: 11
    Last Post: 09-25-2011, 09:26 PM
  4. Issue with nested for loop
    By sunshine64 in forum New To Java
    Replies: 5
    Last Post: 02-03-2011, 02:45 AM
  5. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM

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
  •