0

I am getting a NullPointerException on the following code on the 2nd line of the snippet. I've tried looking it up online but can't fix it. If anyone could please help me it would be greatly appreciated!

for (int j=1;j<12;j++) {
        while (!topCard.getCardDetails().toString().equals("")) {
            hand1[j-1] = (String) topCard.getCardDetails();
        }
            topCard = topCard.getNextCard();
        removeCard(topCard);
        topCard = topCard.getNextCard();
  • You are most probably calling toString() on a null object, try to check the response after calling getCardDetails – Asad Jivani Nov 17 '20 at 16:23
  • *on the 2nd line of the snippet.* - so which variable is null? You have to variables: 1) topCard and 2) the value returned by topCard.getCardDetails(). Once you know which in null you fix the problem. Also, why do you have the while loop. It does nothing. It will be an infinite loop since you change the reference to the "topCard" variable. – camickr Nov 17 '20 at 16:23
  • Actually, `toString()` can return `null` too - but doing so is a bad idea. PS.: Use Java 15, as this has [helpful NullPointerExceptions](https://openjdk.java.net/jeps/358) enabled by default. – Johannes Kuhn Nov 17 '20 at 16:28

0 Answers0