0

I am making a simple blackjack program in java, in which there is a Dealer class that extends the Player class. The problem in the code below is that when calling the Player.hit() method is called, I get an error saying this:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "a" is null

Here's the code for the player class:

package Blackjack;

import java.util.ArrayList;
import java.util.Scanner;

public class Player {
    private ArrayList<Card> hand; //users hand
    private int handvalue = 0;
    private String name;
    private Card[] aHand;
    Player(ArrayList<Card> hand, String name){
        this.hand = hand;
        this.name = name;
    }
    public void addCard(Card card){
        hand.add(card);
    }
    public boolean hasBlackjack(){
        if (handvalue == 21){
            return true;
        }
        else{
            return false;
        }
    }
    Scanner input = new Scanner(System.in);
    public String hitOrStand(){
        System.out.println("Would " + name + " like to hit or stand?\n");
        String choice = input.nextLine();
        return choice;
    }
    public int calcHandValue(ArrayList<Card> hand){
        aHand = hand.toArray(aHand);
        int handvalue=0;
        for(int i=0; i<aHand.length; i++)
        {
            handvalue += aHand[i].getValue();
            if(aHand[i].getValue()==11)
            {
                System.out.println("You have drawn an ace. Would you like it to be worth 11 or 1?");
                int acevalue = input.nextInt();
                if(acevalue == 1){
                    aHand[i].value = 1;
                }
            }
        }
        return handvalue;
    }
    public boolean checkBust(){
        if (handvalue > 21){
            System.out.println(name + " has busted!");
            return true;
        }
        return false;
    }
    public void hit(Deck deck)
    {
        hand.add(deck.drawCard());
        aHand = hand.toArray(aHand);
        handvalue = 0;
        for(int i=0; i<aHand.length; i++)
        {
            handvalue += aHand[i].getValue();
            if(aHand[i].getValue()==11)
            {
                System.out.println("You have drawn an ace. Would you like it to be worth 11 or 1?");
                int acevalue = input.nextInt();
                if(acevalue == 1){
                    aHand[i].value = 1;
                }
            }
        }
    }

    public String getName() {
        return name;
    }

    public ArrayList<Card> getHand() {
        return hand;
    }
}

  • Did you ever assign `aHand` a non-null value before trying to pass it to the `toArray` method? – Kevin Anderson Jan 24 '21 at 13:58
  • 2
    I don't think that the problem is in the code you've posted, because there is no variable called "a" in this code, ... the problem is that somewhere in your code you have a variable "a" which is null. .. can you please post the complete stacktrace? – JavaMan Jan 24 '21 at 13:58
  • I guess you forgot to initialize the object `hand`. – Yun CHEN Jan 24 '21 at 14:00
  • Related: [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/q/3988788) – Pshemo Jan 24 '21 at 14:06

0 Answers0