Note that there are some explanatory texts on larger screens.

plurals
  1. PONull Point Exception Error with simple card class
    primarykey
    data
    text
    <p>I'm a beginner with Java, and I've been making test code to apply some of the concepts I read about. So, I wanted to create a class to describe playing cards. The card class contains fields describing both suite and value (1 - 13 for all 4 suites). This class was very simple to create, as you can see --</p> <pre><code>public class Card { private String suite; private int cardValue; Card(String s, int cV){ this.suite = s; this.cardValue = cV; } public String getSuite(){ return this.suite; } public int getCardValue(){ return this.cardValue; } } </code></pre> <p>I made another class to test this class, as well as add 52 cards to an array (I have another class that will deal with a 52 card deck, but that's not important in the context of my question here). This class, called <code>CardTest</code>, contains the main method. I created a for loop that appends everything to the deck array, however my problems occur when I want to loop through the deck and print out card values (suite and value). I receive a NullPointException error. Here is the cardTest class: </p> <pre><code>public class CardTest { public static void main(String[] args){ Card[] temp = new Card[52]; for (int i = 0; i &lt;12; i++){ temp[i] = new Card("Spade", i + 1); temp[i+13] = new Card("Club", i + 1); temp[i+26] = new Card("Diamond", i + 1); temp[i +39] = new Card("Heart", i + 1); } for (int i = 0; i &lt; 52; i++){ System.out.println(temp[i].getSuite()); } } } </code></pre> <p>I tried to search for issues regarding this type of error, but the only thing I've gathered is that there is an issue with the Card objects being set to a default "null" value, which yields the error on the method call. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload