![]() |
Run time error popping up for my FullDeck arrary class
Hi
I created two classes. one Card class which basically specifies what type of data a Card object would have (Suit values and suit numbers). The other one is the Full Deck class which is basically meant to create 52 Card objects (which consist of all 52 cards in a Full Deck). I have used arrays in Full Deck. BOTH CLASSES COMPILE FINE. WHEN I RUN THE APPLICATION (FULLDECK), it STOPS EXECUTING DUE TO A RUN TIME ERROR! I am a beginner in Java, so I am trying to figure out whats the cause. Can someone please tell what the cause for the runtime error is and what can be done to correct it? The Card class is as follows Code:
Code:
//Importing JOptionPane |
You have specified that cardDeck is a Card[], but you have not instantiated it to anything. Each offset in cardDeck is therefore a null, so you'll be pulling up a NullPointerException on this line:
cardDeck[x].setSuit(suit);. You need to invoke a new Card() first and write it to cardDeck[x].I don't like the iteration's either. Consider using two of simply numFaces and numSuits (13 and 4 in a standard deck ignoring joker). So I'd suggest 2 loops nested of the number of faces and suits, and then keep a separate incremented variable for adding. I'd also suggest adding two constructors to card, one default and one that accepts the face and the suit. Chain them together. Is this for an assignment? If not, I'd also suggest using enums and collections. Any collection will do, but if you treat the deck as a stack or queue, I'd suggest a linkedlist or subclass of such (using deque interface) would be a good idea. Collections also have an added benefit of the shuffle method. |
Thanks for your help Fou-Lou. Your a legend! I understood your first two paragraphs. That helped me correct my code. :) Yes it was for an assignment.
|
corrected source code.Game Zone 17a Chapter 8- Java Programming 6th ed Joyce Farrell
NOTE: The source code has been posted for learning purposes for new programmers like me. Please use it for learning purposes and not for other purposes. Do NOT copy CODE. Learn the concepts and write your own. :)
Card Class Code:
/*Code:
|
| All times are GMT +1. The time now is 11:48 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.