Results 1 to 5 of 5
Thread: Variable not being assigned?
- 03-23-2012, 01:14 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
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.
- 03-23-2012, 01:43 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 03-23-2012, 04:52 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
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.
- 03-23-2012, 04:56 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
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?
- 03-23-2012, 05:11 PM #5
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.
Please post full text of the error message.I encounter an error,If you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
Dynamic variable name based on other variable
By nadissen in forum EclipseReplies: 4Last Post: 05-06-2011, 06:22 PM -
thread ref assigned to null
By rajinder5 in forum Threads and SynchronizationReplies: 2Last Post: 12-22-2010, 07:47 PM -
How do I substitute any variable for a hardcoded variable
By Weazel Boy in forum New To JavaReplies: 11Last Post: 07-07-2010, 06:02 AM -
Variable name determined by another variable's value
By Lumpkabob in forum New To JavaReplies: 5Last Post: 04-14-2009, 08:00 AM -
FInal field cannot be assigned
By ravian in forum New To JavaReplies: 3Last Post: 12-13-2007, 02:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks