Results 1 to 5 of 5
  1. #1
    Evan.C is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Variable not being assigned?

    Hello,
    Im currently trying to use a variation of the NeHe lesson 10, in order to use the SetupWorld section to assign vertex in a textfile to an array. Here's the error bit of code:
    Java Code:
    Sector sector1;
    int numsquares = 0;
    private void setupWorld() {
    		float x,y,z,u,v;
    		
    		try {
    			String line;
    			FileReader fr = new FileReader("C:/Users/EvanClark/workspace/RofT/Data/Cube.txt");
    			BufferedReader dis = new BufferedReader(fr);
    			try {
    				while ((line = dis.readLine()) != null) {
    					if (!(line.trim().length() == 0 || line.trim().startsWith("//")))
    						continue;
    					if (line.startsWith("NumSquares")) {
    						int numSquares;
    						numSquares = Integer.parseInt(line.substring(line.indexOf("NumSquares") + "NumSquares".length() + 1));
    						sector1 = new Sector(numSquares);
    						numsquares = numSquares;
    						break;
    					}
    					
    				}
    					for (int i=0; i<numsquares; i++) {
    					for (int vert = 0; vert < 4; vert++) {
    						while ((line = dis.readLine()) != null) {
    							if (line.trim().length() == 0 || line.trim().startsWith("//")) 
    								continue;
    							break;
    						}
    						if (line != null) {
    							StringTokenizer st = new StringTokenizer(line, " ");
    							sector1.square[i].vertex[vert].x = Float.valueOf(st.nextToken()).floatValue();
    							sector1.square[i].vertex[vert].y = Float.valueOf(st.nextToken()).floatValue();
    							sector1.square[i].vertex[vert].z = Float.valueOf(st.nextToken()).floatValue();
    							sector1.square[i].vertex[vert].u = Float.valueOf(st.nextToken()).floatValue();
    							sector1.square[i].vertex[vert].v = Float.valueOf(st.nextToken()).floatValue();								
    						}
    					}
    				}
    				dis.close();
    			} catch (Exception e) { 
    				System.out.println("SetupWorldException1"); 
    				e.printStackTrace();
    			}
    			
    			}catch (Exception e) {
    				System.out.println("SetupWorldException2");
    				e.printStackTrace();				
    		}
    		hasSetup = true;
    	}

    These bits may be important also, although Im pretty sure there not where the problem lies:

    Java Code:
    public class Sector {
    	public int numSquares;
    	Square square[];
    	public Sector(int num) {
    		numSquares=num;
    		square = new Square[numSquares];
    		for (int i=0; i<numSquares;i++) {
    			square[i] = new Square();
    		}
    	}
    }
    
    public class Vertex {
    	public float x,y,z,u,v;
    }
    
    public class Square {
    	public Vertex vertex[];
    	public Square() {
    		vertex = new Vertex[3];
    		for (int i=0;i<4;i++)
    			vertex[i] = new Vertex();
    	}
    }

    The problem is that when I reference either sector1.numSquares, or numsquares it doesn't work. I attempted to set up a for loop using numsquares after sector1.numSquares didn't work, and told it to output numsquares just before the for loop, and it still said it was 0, despite me having called setupworld.

    I told the system to output "setting up" at the start of setupworld so I know thats not the problem.

    The int numsquares = 0; isn't the problem either, as even when I dont assign a value to it, it doesnt work, and when I use sector1.numSquares it doesn't work either :/

    Thanks,
    Evan.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Variable not being assigned?

    What does "doesn't work" or "didn't work" mean?
    They tell us nothing of any value.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Evan.C is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Variable not being assigned?

    It doesn't set the variable numsquares, or the variable sector1.numSquares. Whenever I attempt to use either variable I encounter an error, or I get the unchanged variable (e.g. with numsquares it will still output 0 and the variable won't be changed after I have told it too).

    The start of the textfile is "NumSquares 6", so surely the variable sector1.numSquares and numsquares should both be 6?


    I'm afraid I can't really give much more code away, as it is for my computing coursework.

    Thanks,
    Evan.

  4. #4
    Evan.C is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Variable not being assigned?

    Infact, just done a couple more tests, it appears that nothing at all is being assigned to sector1, even though setupWorld() is definitely being run through. Any ideas why?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Variable not being assigned?

    Is the code that assigns values to sector1 being executed?
    Is there more than one Sector object? You are assigning values to one and looking in another for the values.

    I encounter an error,
    Please post full text of the error message.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Dynamic variable name based on other variable
    By nadissen in forum Eclipse
    Replies: 4
    Last Post: 05-06-2011, 06:22 PM
  2. thread ref assigned to null
    By rajinder5 in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-22-2010, 07:47 PM
  3. Replies: 11
    Last Post: 07-07-2010, 06:02 AM
  4. Variable name determined by another variable's value
    By Lumpkabob in forum New To Java
    Replies: 5
    Last Post: 04-14-2009, 08:00 AM
  5. FInal field cannot be assigned
    By ravian in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 02:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •