Before you read on, I realize I could've used arraylists to do the job better and probably much much easier, but this is a learning project to use arrays only...like a challenge to myself to become more familiar and comfortable with using arrays in a bigger project than I usually do
I have fleshed out the card game I posted on here throughout this month, and now that it's almost functional, I've come across some problems.
I am fairly new to java still, and to be honest I kind of don't really know if and when I'm going about something the wrong way. So as you read through my code you will maybe notice things that don't really seem to make sense for a seasoned java user, but please understand that I'm kind of trying to make this work with the small amount of tools that I know how to use, so please be aware of that. Also, as a beginner my code is probably not the cleanest.
So I'm getting a null pointer exception on line 97 99 of my card class, and it's leading me to believe that my dealHands method is not doing what it's supposed to, which is populate Player One's hand array with references to Cards in Card.deck.
Anyway here's my four classes (again, it's not the best design or cleanest code, but I'm still learning)
Nothing is withheld from us what we have conceived to do. -Russel Kirsch-
Sorry bout that...I edited the forum post because some lines were too many characters long for the forum but forgot to change the error...
It should be on line 99...it is the if statement inside play method
The error occurs when I try to enter my first int in the console(i.e. when I try to play the game)
Bear Bibeault wrote:OK, so think about what your next debugging step could be. What terms in that statement are subject to NPE's? One strong suspect is p2 (p2? Surely you can think of better names for things!).
How can you find out what's null, but shouldn't be?
I'm unsure. To be honest I haven't gone to school and don't have any background in programming so everything is new to me and I'm kind of "faking it till I make it"
That said, what I can say about that line is that my intention with it is to check that p1Play (the entered int) is equal to any card in Player Two's hand array. && If that's satisfied, then cardUsed must be false (otherwise a player could keep asking for the same King of Hearts over and over), and finally, since the Player Two hand array has certain null indices, it makes sure to disregard those.
That's what I intend with that "if logic" but maybe I could be misunderstanding my own code
My hunch is that there's something wrong with my dealHands method, but I can't be sure and don't know how to use eclipse to isolate the problem(again, I'm new) To be more specific, I'm thinking perhaps the dealHands method is actually not creating references in the hand arrays as I intend them to due to erroneous code.
James Boswell wrote:Justin
From a quick review of your code, it looks like you have not initialised your hand array for the p2 variable (instance of PlayerTwo). Simply setting it to
does not assign any objects to the array.
If you mean the hand arrays in classes PlayerOne and PlayerTwo, the dealHands method in class Card is intended to fill indices 0-7 with references to cards in Card.deck Here is the excerpt
(basically fill the array with card references rather than card objects...however un-proper that may be, again, I have a very limited toolset and understanding about how to achieve what I want to achieve when writing a program. I initially tried to actually take the objects themselves out of the deck array in class Card and place them in the hand arrays in classes PlayerOne and PlayerTwo, but then I was unsure if it would be wise to do that, since it would make null indices in deck, thus requiring even more null-checking all throughout the program)
Well, I think it's back to the drawing board for me and this program...it started off well but got a little messy with the play method, but the important thing is that I learned some new things and got more experience.
Cheers and thanks for taking the time and patience to peruse that mess :P