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;
}
}