Hello, need help programming a blackjack game for my computer science class. I have used a cirularlinkedlist to create this game. I have received a null pointer exception error. I think it has to do with my circularlinkedlist class.

import java.util.NoSuchElementException;

public class CircularLinkedList {
    private ListNode last;

    public CircularLinkedList(){
        last = null;
    }

    public boolean isEmpty(){return last==null;}

    public void add(Card c){
        if(isEmpty()){
            last = new ListNode(c,null);
            last.setNext(last); //MUST be an individual statement
        }
        else{
            last.setNext(new ListNode(c,last.getNext()));
            last = last.getNext();//point to the new last
        }
    }
    /*
     * preconditions: integer i (first ListNode is number 1) is where
     *                object o will be added
     * postconditions: add listNode at place i.
     */
    public void add(int i, Card o){
        if(isEmpty()){//i does not matter
            last = new ListNode(o,null);
            last.setNext(last); 
        }
        if(i==1){// add to beginning
            last.setNext(new ListNode(o,last.getNext()));
        }
        ListNode curr = last.getNext();
        ListNode prev = last;
        int count=0;
        while(curr!=last){//counting ListNodes
            count++;
            if(count==i){
                curr.setNext(new ListNode(o, curr.getNext()));  
            }
            prev = curr;
            curr = curr.getNext();
        }
        last.setNext(new ListNode(o,last.getNext()));
        last = last.getNext();

    }
    public void addFirst(Card c){
        if(isEmpty()){
        last = new ListNode(c,null);
        }
        else{
        last.setNext(new ListNode(c,last.getNext()));
        }
    }
    public Card removeFirst(){
        if(isEmpty())
           throw new NoSuchElementException("list is empty");
        ListNode first = last.getNext();
        if(first==last){//only 1 element
            last = null;
            return first.getValue();
        }
        last.setNext(first.getNext());
        return first.getValue();
    }
    public Card removeLast(){
        if(isEmpty())
             throw new NoSuchElementException("list is empty");
        ListNode curr = last.getNext();
        ListNode prev = last;
        if(curr==last){//only 1 element
            last = null;
            return curr.getValue();
        }
        while(curr!=last){//have to walk through to the end!
            prev = curr;
            curr = curr.getNext();
        }
        prev.setNext(last.getNext());
        last = prev; //point to new last node
        return curr.getValue();
    }
    /*
     * preconditions: integer i (first ListNode is number 1)
     * postconditions: returns card at ListNode i and removes it from CircularLinkedList.
     */
    public Card remove(int i){
        if(isEmpty())
             throw new NoSuchElementException("list is empty");
        ListNode curr = last.getNext();
        ListNode prev = last;
        int count=0;
        if(curr==last && i==1){//only 1 element and i=1
            last = null;
            return curr.getValue();
        }
        while(curr!=last && i != count){
            prev = curr;
            curr = curr.getNext();  
            count++;    
        }   
        prev.setNext(curr.getNext());
        return curr.getValue();

    }

    /*
     * preconditions: integer i (first ListNode is number 1) i must be a possible number
     * postconditions: returns card at ListNode i.
     */
    public Card get(int i){
        if(isEmpty())
             throw new NoSuchElementException("list is empty");

        ListNode curr = last.getNext();
        ListNode prev = last;
        if(curr==last && i==1 || i==1){//only 1 element and i=1
            return curr.getValue();
        }
        int count=0;
        while(curr!=last && count !=i){
            prev = curr;
            curr = curr.getNext();
            count++;
        }
        return curr.getValue();
    }
    /*
     * preconditions: integer i (first ListNode is number 1)
     * postconditions: sets ListNode at integer i to object o/
     */
    public void set (int i, Card o){
        if(isEmpty())
             throw new NoSuchElementException("list is empty");
        ListNode curr = last.getNext();
        ListNode prev = last;
        int count=0;
        while(curr!=last){
            count++;
            prev = curr;
            curr = curr.getNext();
            if (count == i)
                curr.setValue(o);

        }
    }

    public int size(){
        int sz=1;
        if(isEmpty())
            return 0;
        ListNode curr = last.getNext();
        while(curr!=last){
            sz++;
            curr = curr.getNext();
        }
        return sz;
    }
    public String toString(){
        String s="";
        if(isEmpty())
            return "empty";
        ListNode curr = last.getNext();
        while(curr!=last){
            s+=curr.getValue()+"\n";
            curr = curr.getNext();
        }
        s+=curr.getValue(); //have to tack on the last one!
        return s;
    }

}





//card class
import java.awt.Graphics;
import java.awt.*;


public class Card {
    private int rank; // ace=1, jack=11, queen =12 king =13
    private int suit; //hearts = 1; spades= 2; diamonds= 3; clubs = 4
    private Image Img;

    //constructor
    public Card( int s, int r, Image i){
        rank = r;
        suit = s;
        Img= i;
    }
    //mutators
    public void setRank(int r){
        rank = r;
    }
    public void setSuit(int s){
        suit = s;
    }
    public void setImage(Image i){
        Img = i;
    }

    //accessors
    public int getRank(){return rank;}
    public int getSuit(){return suit;}
    public Image getImg(){return Img;}

    public String toString(){
        String s1 = "";
        if(rank == 1)
            s1= "Ace";
        else if(rank == 2)
            s1= "2";
        else if(rank == 3)
            s1= "3";
        else if(rank == 4)
            s1= "4";
        else if(rank == 5)
            s1= "5";
        else if(rank == 6)
            s1= "6";
        else if(rank == 7)
            s1= "7";
        else if(rank == 8)
            s1= "8";
        else if(rank == 9)
            s1= "9";
        else if(rank == 10)
            s1= "10";
        else if(rank == 11)
            s1= "Jack";
        else if(rank == 12)
            s1= "Queen";
        else if (rank ==13)
            s1= "King";

        String s2="";
        if (suit == 1)
            s2= "hearts";
        else if (suit == 2)
            s2= "spades";
        else if (suit == 3)
            s2= "diamonds";
        else if (suit == 4)
            s2="clubs";

        return s1+ " of " +s2 + "\n";

    }
    public void draw(Graphics g){      
        g.drawImage(Img, 0,0,125,150,null,null);
}

}



//deck class

import java.awt.Graphics;
import java.awt.*;
import java.lang.Math.*;

public class Deck {
    //private member variables
    private CircularLinkedList list = new CircularLinkedList();
    private Card h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, hj, hq, hk;
    private Card s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, sj, sq, sk;
    private Card d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, dj, dq, dk;
    private Card c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, cj, cq, ck;
    private int random;

    //birthing all the cards
    {
    h1 = new Card(1, 1, Toolkit.getDefaultToolkit().getImage("./h1.png"));
    h2 = new Card(1, 2, Toolkit.getDefaultToolkit().getImage("./h2.png"));
    h3 = new Card(1, 3, Toolkit.getDefaultToolkit().getImage("./h3.png"));
    h4 = new Card(1, 4, Toolkit.getDefaultToolkit().getImage("./h4.png"));
    h5 = new Card(1, 5, Toolkit.getDefaultToolkit().getImage("./h5.png"));
    h6 = new Card(1, 6, Toolkit.getDefaultToolkit().getImage("./h6.png"));
    h7 = new Card(1, 7, Toolkit.getDefaultToolkit().getImage("./h7.png"));
    h8 = new Card(1, 8, Toolkit.getDefaultToolkit().getImage("./h8.png"));
    h9 = new Card(1, 9, Toolkit.getDefaultToolkit().getImage("./h9.png"));
    h10 = new Card(1, 10, Toolkit.getDefaultToolkit().getImage("./h10.png"));
    hj = new Card(1, 11, Toolkit.getDefaultToolkit().getImage("./h11.png"));
    hq = new Card(1, 13, Toolkit.getDefaultToolkit().getImage("./h12.png"));
    hk = new Card(1, 13, Toolkit.getDefaultToolkit().getImage("./h13.png"));

    s1 = new Card(2, 1, Toolkit.getDefaultToolkit().getImage("./s1.png"));
    s2 = new Card(2, 2, Toolkit.getDefaultToolkit().getImage("./s2.png"));
    s3 = new Card(2, 3, Toolkit.getDefaultToolkit().getImage("./s3.png"));
    s4 = new Card(2, 4, Toolkit.getDefaultToolkit().getImage("./s4.png"));
    s5 = new Card(2, 5, Toolkit.getDefaultToolkit().getImage("./s5.png"));
    s6 = new Card(2, 6, Toolkit.getDefaultToolkit().getImage("./s6.png"));
    s7 = new Card(2, 7, Toolkit.getDefaultToolkit().getImage("./s7.png"));
    s8 = new Card(2, 8, Toolkit.getDefaultToolkit().getImage("./s8.png"));
    s9 = new Card(2, 9, Toolkit.getDefaultToolkit().getImage("./s9.png"));
    s10 = new Card(2, 10, Toolkit.getDefaultToolkit().getImage("./s10.png"));
    sj = new Card(2, 11, Toolkit.getDefaultToolkit().getImage("./s11.png"));
    sq = new Card(2, 13, Toolkit.getDefaultToolkit().getImage("./s12.png"));
    sk = new Card(2, 13, Toolkit.getDefaultToolkit().getImage("./s13.png"));

    d1 = new Card(3, 1, Toolkit.getDefaultToolkit().getImage("./d1.png"));
    d2 = new Card(3, 2, Toolkit.getDefaultToolkit().getImage("./d2.png"));
    d3 = new Card(3, 3, Toolkit.getDefaultToolkit().getImage("./d3.png"));
    d4 = new Card(3, 4, Toolkit.getDefaultToolkit().getImage("./d4.png"));
    d5 = new Card(3, 5, Toolkit.getDefaultToolkit().getImage("./d5.png"));
    d6 = new Card(3, 6, Toolkit.getDefaultToolkit().getImage("./d6.png"));
    d7 = new Card(3, 7, Toolkit.getDefaultToolkit().getImage("./d7.png"));
    d8 = new Card(3, 8, Toolkit.getDefaultToolkit().getImage("./d8.png"));
    d9 = new Card(3, 9, Toolkit.getDefaultToolkit().getImage("./d9.png"));
    d10 = new Card(3, 10, Toolkit.getDefaultToolkit().getImage("./d10.png"));
    dj = new Card(3, 11, Toolkit.getDefaultToolkit().getImage("./d11.png"));
    dq = new Card(3, 13, Toolkit.getDefaultToolkit().getImage("./d12.png"));
    dk = new Card(3, 13, Toolkit.getDefaultToolkit().getImage("./d13.png"));

    c1 = new Card(4, 1, Toolkit.getDefaultToolkit().getImage("./c1.png"));
    c2 = new Card(4, 2, Toolkit.getDefaultToolkit().getImage("./c2.png"));
    c3 = new Card(4, 3, Toolkit.getDefaultToolkit().getImage("./c3.png"));
    c4 = new Card(4, 4, Toolkit.getDefaultToolkit().getImage("./c4.png"));
    c5 = new Card(4, 5, Toolkit.getDefaultToolkit().getImage("./c5.png"));
    c6 = new Card(4, 6, Toolkit.getDefaultToolkit().getImage("./c6.png"));
    c7 = new Card(4, 7, Toolkit.getDefaultToolkit().getImage("./c7.png"));
    c8 = new Card(4, 8, Toolkit.getDefaultToolkit().getImage("./c8.png"));
    c9 = new Card(4, 9, Toolkit.getDefaultToolkit().getImage("./c9.png"));
    c10 = new Card(4, 10, Toolkit.getDefaultToolkit().getImage("./c10.png"));
    cj = new Card(4, 11, Toolkit.getDefaultToolkit().getImage("./c11.png"));
    cq = new Card(4, 13, Toolkit.getDefaultToolkit().getImage("./c12.png"));
    ck = new Card(4, 13, Toolkit.getDefaultToolkit().getImage("./c13.png"));
    }

    public Deck(){
        //hearts
        list.add(h1);
        list.add(h2);
        list.add(h3);
        list.add(h4);
        list.add(h5);
        list.add(h6);
        list.add(h7);
        list.add(h8);
        list.add(h9);
        list.add(h10);
        list.add(hj);
        list.add(hq);
        list.add(hk);
        //spades
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);
        list.add(s6);
        list.add(s7);
        list.add(s8);
        list.add(s9);
        list.add(s10);
        list.add(sj);
        list.add(sq);
        list.add(sk);
        //diamonds
        list.add(d1);
        list.add(d2);
        list.add(d3);
        list.add(d4);
        list.add(d5);
        list.add(d6);
        list.add(d7);
        list.add(d8);
        list.add(d9);
        list.add(d10);
        list.add(dj);
        list.add(dq);
        list.add(dk);
        //clubs
        list.add(c1);
        list.add(c2);
        list.add(c3);
        list.add(c4);
        list.add(c5);
        list.add(c6);
        list.add(c7);
        list.add(c8);
        list.add(c9);
        list.add(c10);
        list.add(cj);
        list.add(cq);
        list.add(ck);
    }
    public Card deal(){
        Card removed;
        random=(int)((Math.random())*52);
        removed= list.remove(random);
        return removed;
    }

    public String toString(){
        String s="";
        for(int i= 1; i <= 52; i++)
            s= " " + list.get(i).toString();
        return s;
    }

}




//hand class

import java.awt.*;
import java.util.ArrayList; 

public class Hand {
    private ArrayList cards= new ArrayList();
    private boolean isConcealed; 


    public Hand(Deck d, boolean b){
        isConcealed=b;
        cards.add(d.deal());
        cards.add(d.deal());
    }
    public void setIsConcealed(boolean i){
        isConcealed=i;
    }
    //finds sum
    public int evaluate(){
        int sum = 0;
        for(int i= 0; i<cards.size(); i++){
            sum += ((Card)(cards.get(i))).getRank();
        }
        return sum;
    }

    public void insert(Deck d){
        cards.add(d.deal());
    }

    public void draw(Graphics g){
        if(isConcealed){
            for(int i= 0; i<cards.size(); i++){
                g.drawImage(Toolkit.getDefaultToolkit().getImage("./b1fv.png"), 0, 0, 125, 150, null, null);
            }

        }
        else{
            for(int i= 0; i<cards.size(); i++){
                g.drawImage(((Card)(cards.get(i))).getImg(), 0,0,125,150,null,null);
            }
        }
    }

    public String toString(){
        String s="";
        for(int i= 0; i<cards.size(); i++){
            s +=((Card)( cards.get(i))).toString();
        }
        return s;
    }
}




//game gui
mport javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.*;


    public class Game extends JFrame implements ActionListener {

        private int WIDTH = 400, HEIGHT = 300;
        private JButton hitButton   = new JButton("Hit Me");
        private JButton stickButton = new JButton("Stick");
        private JButton dealButton  = new JButton("Deal");
        private JLabel status = new JLabel(" ", JLabel.CENTER);
        private Graphics g;



        public Game() {
            setTitle("Blackjack");
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // hit, stick, and deal buttons
            JPanel buttons = new JPanel();
            buttons.add(hitButton);
            buttons.add(stickButton);
            buttons.add(dealButton);
            hitButton.addActionListener(this);
            stickButton.addActionListener(this);
            dealButton.addActionListener(this);
            hitButton.setEnabled(false);
            stickButton.setEnabled(false);

            if (dealButton.isSelected()){
                hitButton.setEnabled(true);
                stickButton.setEnabled(true);
            }

            // add the dealer, gambler, buttons, and status bar
            getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
            getContentPane().add(buttons);
            getContentPane().add(status);
            pack();
            setVisible(true);
        }

        private void checkWinner(Hand p, Hand d) {
            d.setIsConcealed(true);        // time to reveal dealer's card
            d.draw(g);
            if      (p.evaluate() >  21)             
                status.setText("Gambler busts");
            else if (d.evaluate()  >  21)             
                status.setText("Dealer busts");
            else if (p.evaluate() == d.evaluate()) 
                status.setText("Push");
            else if (p.evaluate() >  d.evaluate()) 
                status.setText("Gambler wins");
            else                                        
                status.setText("Dealer wins");
            System.out.println(status.getText());
        }

        // deal one card to given player from the deck
        private void hit(Hand person, Deck d) {
            person.insert(d);
            person.draw(g);
        }

        public void deal(Hand  player, Hand Dealer){
            player.draw(g);
            Dealer.draw(g);
        }

        public void actionPerformed(ActionEvent e) {
            Deck d = new Deck();
            Hand player = new Hand(d, false);
            Hand dealer = new Hand(d, true);

            if (e.getSource() == hitButton) {
                hit(player,d);
                if(player.evaluate() > 21) {
                    checkWinner(player, dealer);
                    hitButton.setEnabled(false);
                    stickButton.setEnabled(false);
                    dealButton.setEnabled(true);
                }
            }

            if (e.getSource() == stickButton) {
                while(dealer.evaluate() < 17)
                    hit(dealer,d); //dealer only hits if under 17
                checkWinner(player, dealer);
                hitButton.setEnabled(false);
                stickButton.setEnabled(false);
                dealButton.setEnabled(true);
            }

            if (e.getSource() == dealButton) {
                deal(player, dealer);
                status.setText(" ");
                hitButton.setEnabled(true);
                stickButton.setEnabled(true);
                dealButton.setEnabled(false);
            }
        }





        // play Blackjack
        public static void main(String argv[]) {
            new Game();
        }



}

Anything at all will help!

thank you.

Could you give us the error message? The java compiler usually tells you what line a NullPointerException is on, and that could help with debugging.

Also, you should use [code] tags to make your post more readable.

error message:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Hand.draw(Hand.java:39)
at Game.deal(Game.java:75)
at Game.actionPerformed(Game.java:104)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

So, your deal method uses Graphics g, which I don't think has been initialized, which I think is why you're getting the NullPointerException.

Besides that, I don't think you're drawing things to the screen correctly. Usually, when you want to draw to the screen, you override the public void paint(Graphics g) method, and the graphics object is passed into the method on it's own, and you don't have to create your own Graphics variable like you do with those draw methods. Look here for more info: http://java.sun.com/products/jfc/tsc/articles/painting/#callback

how do you initialize Graphics?

I don't even think you can initialize Graphics (not sure on this though). You want to change your draw methods to paint methods. That way they're invoked automatically like in the link that I showed you. All of your painting to the screen should be done in that method, and then you can update it with the repaint() function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.