0

This is a follow up question from Problem with array assignment

I now have addcube done like so.. and all works as expected, when I print the array. but when I print the same index's AFTER assignment in another class It tells me their equal to 0. So the values are not 'saving'. Why is this? How would I correct this?

       public void addcube(float highx, float lowx, float highz, float lowz){
        //Constructing new cube...
System.out.println("f = " + f);
        Global.cubes++;
        float y = 1.5f;
        System.out.println("highx = " + highx + "lowx = " + lowx + "highz = " +         highz + "lowz = " + lowz);
        //FRONT
        Global.camObjCoord[Global.i] = highx;
        Global.i++;
        System.out.println("cube i = " + Global.i);
}

In both cases I'm printing like so...

    int p = 0;
            while(p < 72){
                System.out.println(Global.camObjCoord[p]);
                p++;
            }

Global.i = 0 at the beginning.

The only other places the array is being referenced is the following..

cubeBuff = makeFloatBuffer(Global.camObjCoord);

                FloatBuffer makeFloatBuffer(float[] arr) {
            ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4);
            bb.order(ByteOrder.nativeOrder());
            FloatBuffer fb = bb.asFloatBuffer();
            fb.put(arr);
            fb.position(0);
            return fb;
    }

There is no further refrences to the array in my code.

Thanks.

Community
  • 1
  • 1
Skizit
  • 43,506
  • 91
  • 209
  • 269
  • 2
    We can't tell, because you haven't shown the code that's doing the printing. I very much doubt that it's failing to store the data. I rather suspect you're printing a different index. – Jon Skeet Jul 13 '10 at 16:52
  • 1
    @Meowmix: And what is `Global.i` before the call to `addcube`? You're still leaving us guessing. If you could post a short but *complete* program, I'm sure the reason will become obvious - but you really, really should stop using global variables like this. – Jon Skeet Jul 13 '10 at 16:59
  • I've updated it with what Global.i is at the start. I think a complete program would be a little large. I'm using the array to dynamically render OpenGL cubes. – Skizit Jul 13 '10 at 17:14
  • 1
    this is now the fourth question about the same addcube() function. perhaps there is a larger issue here that just indices? – matt b Jul 13 '10 at 17:39
  • @Meowmix: A complete program doesn't *have* to be large. I'm not suggesting posting it as it is - just enough to demonstrate the problem. – Jon Skeet Jul 13 '10 at 17:56

2 Answers2

2

I would seriously question your design. You're always refering to that Global class, which apparantly seems to be changed from everywhere, and hence you run into such problems (for instance previously with your NullPointerException).

Try seperate things clearly using encapsulation and do not just use one global state that is operated on by different classes. If classes strictly operate only on their own members then dependencies are reduced and it is much easier to track where data is manipulated.

Community
  • 1
  • 1
Janick Bernet
  • 20,544
  • 2
  • 29
  • 55
1

My guess is that your code looks something like this:

System.out.println(Global.camObjCoord[Global.i]);
addcube(/* values here */);
System.out.println(Global.camObjCoord[Global.i]);

and it's printing out 0. Well, that's not printing out the same index after assignment, because Global.i changes value during addcube. For example, suppose Global.i is 3 before the call to addcube. The call to addcube will set Global.camObjCoord[3] to a value, but then set Global.i to 4, so the final line will print out Global.camObjCoord[4] - i.e. not the value which is just been set.

This sort of thing is precisely why global variables are a bad idea...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194