Results 1 to 13 of 13
Thread: Static Vector Problem
- 11-09-2008, 10:32 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
- 11-09-2008, 10:34 PM #2
Member
- Join Date
- Oct 2008
- Location
- Aberystwyth
- Posts
- 55
- Rep Power
- 0
Woah. more information is needed i think in what exactly you're trying to do. If you post for help like that its unlikely people will answer you.
-
1) why is the Vector static? Why not make it an instance variable?
2) It sounds like you don't really want to copy the data and that's it, you want to listen to the data for changes which suggests using the Observable design pattern. If using Swing, you may want to look into using a ChangeListener here.
- 11-09-2008, 10:41 PM #4
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
Well I am making a card game. I have a static vector that is for the players hand. Then in a separate class I have a method for replacing all of the 11's 12's 13's and 1's with JQKA respectively. I copy the contents of the static vector to a different vector using a for loop. When the static vector changes (the one that is for keeping track of the hand) the display does not change. I know the original vector is changing but I dont think that the second class is getting a the changed vector, it just keeps the old one. Im sorry if that is confusing or too vague. I can post a bit of code if it would help. Thanks a bunch
- 11-09-2008, 10:45 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
When I dont have the vectors static i get out of bounds errors. I dont know why but it works like this :confused: What I wanted to do was copy the new data when the vector is changed, when I call the method.
-
Again, why are you making this a static variable? This sounds foolish at best and dangerous at worst. If you have more than one player, are they all sharing the same hand? Do you really want to throw OOPs out the window here?
Yep, you need to use the Observable pattern here. Again a ChangeListener may be all you need. You can read up on it on the Sun tutorials and the API.Then in a separate class I have a method for replacing all of the 11's 12's 13's and 1's with JQKA respectively. I copy the contents of the static vector to a different vector using a for loop. When the static vector changes (the one that is for keeping track of the hand) the display does not change.
-
Ah, OK, now I see. Fixing a bug with a kluge is not a good reason, I'm afraid. The first thing you need to do is to fix this bug.When I dont have the vectors static i get out of bounds errors. I dont know why but it works like this
- 11-09-2008, 11:01 PM #8
Could that be because there are more than one vector and some of them are empty.When I dont have the vectors static i get out of bounds errors
When its static, there is only one object.
What does your program design require? Can each class have its own version of the vector or do they all need to see the one and only version?
If each can have their own version, then it would be better if the names were unique.
- 11-09-2008, 11:06 PM #9
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
Some actual code would be helpful. Perhaps the topic creator isn't properly instantiating objects that create these problems?
- 11-09-2008, 11:08 PM #10
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
I am making a black jack game. I have two vectors phand for the player and dhand for the dealer. When I start the game both of these vectors are empty. I then use a method in a seperate class to add up totals and replace the facecards for the display. When I do not have the vectors as static, the adding class is passed a blank vector, and when I add cards to the hands the adding class does not see these changes.
-
so... you've got a bug. Time for some debugging then. I recommend you sprinkle your code with println's to check to see what's going on, either that or use a decent debugger.
- 11-09-2008, 11:17 PM #12
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
I know why it is not passing it the right vector initially. When my adding class is created it gets its own version of each vector, which is blank at the time it gets it. When I try to call the vector later it calls its own version of the vector, which was blank.
- 11-09-2008, 11:31 PM #13
Member
- Join Date
- Nov 2008
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
Vector problem
By Ace_Of_John in forum New To JavaReplies: 1Last Post: 01-27-2008, 08:53 PM -
vector problem
By mambo_jumbo in forum New To JavaReplies: 1Last Post: 11-17-2007, 10:44 PM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM -
Problem with vector, java.lang.ClassCastException
By paul in forum New To JavaReplies: 1Last Post: 07-16-2007, 04:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks