• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

My arrays are getting destroyed??!

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi gang, I'm currently building a cribbage game and am running into a weird issue with 2 arrays getting mysteriously set to null. First some code, then an explanation:

snippet from GUI class:


from gameFlow class:

From Hand class:



Basically what's happening here is when the user clicks the "Deal cards" button, the actionperformed method on the GUI class gets called and creates a "gameFlow" object. The gameFlow object handles the mechanics of 1 round of cribbage. (dealing, discarding, pegging, counting, etc) In the constructor of the gameFlow class you'll see that it creates a Hand object, initializing it to a variable called "hand1."
In the constructor of the Hand class, it creates a number of different variables, including 2 Card[] arrays with a size of 6 called playerHand and compHand. These represent 2 different cribbage hands.

**Here's the problem**: Immediately after the arrays are initialized in the Hand constructor, program execution goes back to the gameFlow class. At this point the arrays somehow get set to null. Then, a bit later in the program it tries to deal cards to populate the arrays. This throws a null pointer exception because the arrays are null - even though they were originally initialized to a size of 6.
Using JBuilder I've stepped through each line of the program and I can see the arrays getting initialized in the Hand constructor, then getting set to null once execution goes back to the gameFlow class. But I can't for the life of me figure out why. My first thought was that maybe it was the GC destroying my arrays, but you'll notice that there's another Card object created in the constructor called "upcard." This doesn't get set to null, only the 2 arrays do.
I'm guessing there's a simple explanation for what's happening, but I'm absolutely baffled. Thanks in advance for anyone who can offer some help!!
[ September 09, 2004: Message edited by: Alex McCormick ]
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The declarations in bold are private member variables.
The declarations in italics are local variables in the Hand constructor.
To achieve the desired behaviour remove Card[] from the two statements in the constructor. These are currently masking the member variables you want to use.

Jules
 
Alex McCormick
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, that fixed it - thank Julian! I feel a bit dumb now, but oh well. Would there ever be a reason why you'd want to use the syntax I did? (by putting Card[] in front of the variable names) I'm not sure I fully understand how they are "masked", other than seeing the undesired behavior in my program.
Thanks again!
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically you are redeclaring them as local variables when you use the Card[] in front of them again and give them the same name. This is what is meant by masking. Whenever you use a local variable that has a same name as a member the local will take precedence (at least I'm pretty sure the local always takes precedence I'm sure if I don't have that quite right someone will correct me)
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the variable with the narrowest scope takes precedence.
 
Alex McCormick
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I think I've got it now. So basically I had 2 sets of array variables - one set were the member variables (class fields) and the other set were local variables. When I was debugging and saw the arrays getting initialized, I was actually seeing the local variables getting initialized, which is why they were set to null once the execution flow left the Hand constructor.
The member variables never actually did get initialized, only declared.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic