aspose file tools*
The moose likes Java in General and the fly likes Null Pointer Exception Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Null Pointer Exception Help" Watch "Null Pointer Exception Help" New topic
Author

Null Pointer Exception Help

Justin Coombs
Greenhorn

Joined: Oct 10, 2012
Posts: 16

Hey again and Merry Christmas

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-
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Not possible; line 97 is a comment. Did you not post the entire file? If not, line #'s that don;t correspond to your post don't help us help you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Justin Coombs
Greenhorn

Joined: Oct 10, 2012
Posts: 16

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

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?
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

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.

See the java tutorial: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Justin Coombs
Greenhorn

Joined: Oct 10, 2012
Posts: 16

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)
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

Yes but is dealHands ever called on the local variable p2 in the play method?
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

Happy Xmas one and all!
Justin Coombs
Greenhorn

Joined: Oct 10, 2012
Posts: 16

Indeed!

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null Pointer Exception Help
 
Similar Threads
Error
illegal start of expression
Main class not recognized
Compiler "cannot find symbol" for my ArrayList
Blackjack program not good with random cards