-1

I have to create 3 class (Card, Deck and Tester) to play a game of card. The rules are easy, just compare two cards randomly from the deck and whichever one is higher is the winner. It runs for the entire deck. Here's my card class (I exclude my comments)

public class Card {
private int value; 
private String suit; 

public Card(){ 
    value =1;

    suit = "clubs";
}
public Card(int n, String s) { 
    n = value; 
    s = suit;
}
public int getValue() { 

    return value; 
}
public void setValue(int n) { 
    if ((n >0) && (n<14)) {
        n = value;
    }
    else { 
        return;
    }
}
public String getSuit() { 
    return suit; // return the string
}
public void setSuit(String s) { 
    if ((s.equals("clubs") || (s.equals("hearts") || (s.equals("diamonds") || (s.equals("spades")))))){
        s = suit;
    }
    else { 
        return;
    }
}
public int rank() { 
    int rank=0;
    int suitVal=0;
    if (suit.equals("clubs")) { 
        suitVal =1;
    }
    else if(suit.equals("diamonds")) { 
        suitVal =2;
    }
    else if(suit.equals("hearts")) { 
        suitVal =3;
    }
    else if(suit.equals("spades")) { 
        suitVal =4;
    }
    rank= (4*(value-1)) + suitVal; 
    return rank; 

 }

}


Deck class:

import java.util.ArrayList; 
     public class Deck { 
           int[] value= {1,2,3,4,5,6,7,8,9,10,11,12,13}; 
           String[] suit= {"clubs", "diamonds", "hearts", "spades"}; 
           public ArrayList<Card> card = new ArrayList<Card>();

      public Deck() { // make default constructor 
           for (int a=0; a< value.length; a++) { 
           for (int b=0; b< suit.length ; b++){ 
                card.add(new Card(value[a], suit[b]));  
         }
    }
}


public Card drawCard() {
    int number = (int) Math.random()* (card.size());
    Card temp = card.get(number);
    card.remove(number);
    return temp;
    }

Tester:

public class Tester {

public static void main(String[] args) {

    Deck deck1 = new Deck();
    Card x = deck1.drawCard();
    Card y = deck1.drawCard();

    int point1=0;
    int point2=0;
    int player=x.rank();
    int comp=y.rank();
    for (int i=0; i<52; i++) {
        if (player > comp) {
            point1++;
        }
        else {
            point2++;
        }
    }
    if (point1 > point2) {
        System.out.println("Player is the winner");
    }
    if (point1 < point2) {
        System.out.println("Computer is the winner");
    }

}   

When I run it it says "Null pointer Exception" and point at line 42 of Card class and line 14 of Tester class. Please help

user2357112
  • 260,549
  • 28
  • 431
  • 505

2 Answers2

1

In the card class,the assignment in parmeterized constructor is wrong, you have the parameters as n and s and this value you should assign to value and suit. But you are doing other way,

public Card(int n, String s) { 
n = value; 
s = suit;
}

Instead of that, you should assign the values of the parameter to value and suit, something like this.

public Card(int n, String s) { 
value = n; 
suit = s;
}

This is the reason the value and suit is default all the time, you never changed it.

Shivaraj
  • 400
  • 5
  • 16
0

hello and welcome to SO @Elvin.

As mentioned in the comments the NPE is telling you where the problem occurs.

Your "suits" in if (suit.equals("clubs")) is null.

The problem is in your card constructor method:

public Card(int n, String s) { 
    n = value; 
    s = suit;
}

you have it the oposite way, it should be because your attributes are value and suit, so you should "store" the new values in them:

public Card(int n, String s) { 
    value = n; 
    suit = s;
}

You seem to be new, try to understand always the message of the error and of course, debug ;)

armengedon
  • 63
  • 10
  • you should open a new question or debug, really, debug is life in programming. Now you will think, come on, its just a small program! but this small programs are intended for you to learn from your mistakes, and debugging will make your life so much easier. Regarding your question, take a look are when the drawCard is called and what happens inside the for and you will see ;) – armengedon Dec 10 '18 at 21:54