I am trying to recreate the push up playing card game in android sdk. For those who are unfamiliar, the push up game is where you draw a card from a deck of normal playing cards and, based on the suit and rank, you have to perform a set of a certain kind of push up. For example, drawing a 5 of diamonds would require you to complete 5 diamond style(hands closed together) push ups. The suit and corresponding push up type are listed as follows: Heart - Regular Diamond - Diamond Club - Staggered Spade - Wide arm (you can read more about the game over here: http://www.nexercise.com/2012/12/fitness-game-the-push-up-game/)
I have yet to implement images of the playing cards so, for now, I am just trying to get the text to display properly. Basically, once the app starts, I want the user to be able to push the button to generate a new card which will then print a message on the screen stating the number and type of push ups to be completed by the user.
Anyway, here is the code I've worked on so far:
public class Card {
public enum Suit {
HEARTS, DIAMONDS, CLUBS, SPADES
}
public enum Rank {
ACE, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, JACK, QUEEN, KING
}
public enum PushUp {
REGULAR, DIAMOND, STAGGERED, WIDEARMED
}
private Suit mSuit;
private Rank mRank;
private PushUp mPushUp;
public Card(Suit mSuit, Rank mRank, PushUp mPushUp) {
this.mSuit = mSuit;
this.mRank = mRank;
this.mPushUp = mPushUp;
}
public Suit getSuit() {
return mSuit;
}
public void setSuit(Suit suit) {
mSuit = suit;
}
public void setRank(Rank rank) {
mRank = rank;
}
public void setPushUp(PushUp pushUp) {
mPushUp = pushUp;
}
public Rank getRank() {
return mRank;
}
public PushUp getPushUpType() {
return mPushUp;
}
@Override
public String toString() {
return "Card:" + mRank.name() + "of" + mSuit.name() + "/n" +
"Complete:" + mRank.name() + "of" + mPushUp.name() + "push ups.";
}
The card class uses 3 different enums(suit, rank, push up type) to create the card object. The class has a toString method to convert the card information into a string.
public class Deck {
private Card[] deck;
public Deck() {
this.deck = new Card[52];
for (int i = 0; i < deck.length; i++)
for (Card.Suit s : Card.Suit.values())
for (Card.PushUp p : Card.PushUp.values())
for (Card.Rank r : Card.Rank.values()) {
Card c = new Card(s, r, p);
this.deck[i] = c;
}
}
public Card pickACard() {
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(this.deck.length);
Card card = this.deck[randomNumber];
return card;
}
public String setText() {
String text = pickACard().toString();
return text;
}
}
The deck class creates an array filled with all the different cards. The deck class has a method to draw a random card and a method to pull the text from that random card.
public class MainActivity extends AppCompatActivity {
private Button mButton;
private TextView mTextView;
private Deck mNewDeck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView);
mButton = (Button) findViewById(R.id.button);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = "";
text = mNewDeck.setText();
mTextView.setText(text);
}
};
mButton.setOnClickListener(listener);
}
}
the main activity class is the main screen that pops up when the app starts. it has a button and a text view. The button should generate a new card and new string every time it's pressed.
When I try to run the app in the emulator, I get the following error:
Blockquote
FATAL EXCEPTION: main Process: com.example.aman.pushupgame, PID: 3325 java.lang.NullPointerException: Attempt to invoke virtual >method 'java.lang.String com.example.aman.pushupgame.Deck.setText()' on a null >object reference at com.example.aman.pushupgame.MainActivity$1.onClick(MainActivity.java:29)
My biggest concern at the moment is getting the code to run as I'm not sure how to fix this error. I am fairly new to both java and android and I am doing this project as a learning experience to reinforce the concepts I have learned thus far. Any help is much appreciated.