Results 1 to 4 of 4
- 03-21-2010, 03:23 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
problem with nullPointerException
Hi guys! I made my first program with java, it's about making a class for a single card and another one for a full deck of cards and then testing it. The compiler's message is: Exception in thread "main" java.lang.NullPointerException
at Cardgames.Deck$BDeck.<init>(Deck.java:26)
at Cardgames.Deck.main(Deck.java:50)
Java Result: 1
here's the code:
please help :)Java Code:package Cardgames; public class Deck { public class Card { public int bySuit=0; public int byRank=1; String stLabel="A\u2663"; public Card(int parSuit,int parRank,String parLabel){ bySuit=parSuit; byRank=parRank; stLabel=parLabel; } private void printCard() { System.out.println(stLabel); return; } } static public class BDeck{ Card[] BDCards = new Card[52]; int c=0; public BDeck(String[] parCardLabels){ for(int i=0;i<4;i++){ for(int j=1;j<14;j++){ BDCards[c].byRank=i; BDCards[c].bySuit=j; BDCards[c].stLabel=parCardLabels[c]; c++; } } c=0; } public void presentDeck(){ for(int i=0;i<4;i++){ System.out.println("\n"); for(int j=1;j<14;j++){ System.out.println(BDCards[c].stLabel + " "); c++; } } return; } } public static void main(String[] args) { String[] stCardLabels = {"A\u2663","2\u2663","3\u2663","4\u2663","5\u2663","6\u2663","7\u2663","8\u2663","9\u2663","10\u2663","J\u2663","Q\u2663","K\u2663","A\u2666","2\u2666","3\u2666","4\u2666","5\u2666","6\u2666","7\u2666","8\u2666","9\u2666","10\u2666","J\u2666","Q\u2666","K\u2666","A\u2665", "2\u2665","3\u2665","4\u2665","5\u2665","6\u2665","7\u2665","8\u2665","9\u2665","10\u2665","J\u2665","Q\u2665","K\u2665","A\u2660","2\u2660","3\u2660","4\u2660","5\u2660","6\u2660","7\u2660","8\u2660","9\u2660","10\u2660","J\u2660","Q\u2660","K\u2660"}; BDeck myDeck = new BDeck(stCardLabels); myDeck.presentDeck(); } }
Moderator Edit: Code tags addedLast edited by Fubarable; 03-21-2010 at 03:48 PM. Reason: Moderator Edit: Code tags added
-
Creating an array of Card doesn't actually create Card objects but rather creates an array of Card variables which you have to fill with Card objects before you can use them. It's like creating a parking lot: you first have to fill it with cars before you can choose one and then drive it.
So, consider adding the line:
You also might want to make this more OOP and make your Card suit and rank fields private with the appropriate constructor and / or getter and setter methods.Java Code:Card[] BDCards = new Card[52]; int c=0; public BDeck(String[] parCardLabels){ for(int i=0;i<4;i++){ for(int j=1;j<14;j++){ // *** add this line *** BDCards[c] = new Card(); BDCards[c].byRank=i; BDCards[c].bySuit=j; BDCards[c].stLabel=parCardLabels[c]; c++; } }
Also, please read my signature about using code tags. Best of luck!
- 03-21-2010, 05:22 PM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Actually, because of how your Card constructor is written, you need to do this:
Since this is your first Java program, I would offer some recommendations:Java Code:public BDeck(String[] parCardLabels){ for(int i = 0; i < 4; i++){ for(int j = 1; j < 14; j++){ BDCards[c] = (new Deck()).new Card(j, i, parCardLabels[c]); c++; } } c = 0; }
1. Keep each class in a separate source file -- class Deck in Deck.java, class Card in Card.java, etc.
2. I'm not sure what "by" means in the Card instance variables "byRank" and "bySuit" -- I think it would be better if those ivars were simply "rank" and "suit".
3. Same goes for "st" and "par" prefixes -- I'm guessing "par" is "parameter" and "st" maybe "static" or something? Don't use them. Stick with simple but descriptive nouns for variable names. If it's a question of confusing an instance variable with a passed argument, you can use the "this" keyword to clarify.
I know lots of experts think this is confusing, and advise against doing it this way. I tend to disagree, and think it's less confusing than having more than one name for what's essentially the same piece of information.Java Code:public class Card { private int suit; private int rank; public Card(int rank, int suit) { this.rank = rank; this.suit = suit; } }
Cards have no business changing their rank or suit once they've been created, so I think the constructor you have is pretty good, except the "label" is a function of the rank and suit. It wouldn't make sense to have a label that's different from the rank and suit, so don't allow such a thing in the constructor. Instead use the constructor I showed above, and write a getLabel() method.
Those Unicode characters aren't working on my console (Mac OSX 10.6, en_US.UTF-8) and I'm not sure why, but anyway, consider defining them as constants in your Card class:
...etc.Java Code:public class Card { ... public static final String CLUB = "\u2663";
Then you can do:
Just a few things to think about. Hope that helps!Java Code:public class Card { ... private String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; private String[] suits = {DIAMOND, SPADE, HEART, CLUB}; ... public String getLabel() { return ranks[rank] + suits[suit]; } ... }
-Gary-
- 03-22-2010, 04:25 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
Thank you for your time and advice guys I appreciate it. I made Card and BDeck classes into separate files and initialized the array objects like you said, everything works like a charm :). Gcalvin you probably can't see the unicode characters because they're unicode - 16 not 8. That's interesting. How will I make characters like that be displayed on all machines and why did you propose I turn them into constants? Another question is: Why did you write :
BDCards[c] = (new Deck()).new Card(j, i, parCardLabels[c]);
instead of just :
BDCards[c] = new Card(j,i,parCardLabels[c]);
?
And one last thing, when I have my classes and my main method in the same file I get the message "non-static variable cannot be referenced from a static context" when I try to create a new instance of a class. Why?
Similar Threads
-
NullPointerException() problem
By mollybaba4 in forum New To JavaReplies: 1Last Post: 02-02-2010, 03:25 PM -
Why do I get a NullPointerException?
By nessa203 in forum New To JavaReplies: 5Last Post: 01-07-2010, 01:14 PM -
NullPointerException problem
By Kris in forum New To JavaReplies: 4Last Post: 10-01-2009, 02:34 PM -
nullPointerException problem
By conandor in forum NetworkingReplies: 1Last Post: 08-14-2007, 01:22 PM -
NullPointerException problem
By warship in forum AWT / SwingReplies: 5Last Post: 08-10-2007, 04:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks