• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Null Pointer Exception Help

 
Ranch Hand
Posts: 31
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)




 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Justin Coombs
Ranch Hand
Posts: 31
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 31
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes but is dealHands ever called on the local variable p2 in the play method?
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Happy Xmas one and all!
 
Justin Coombs
Ranch Hand
Posts: 31
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic