Recieving a NullPointerException upon executing a method?
I am trying to find a creative way to make a BlackJack application with a
deck of 56 cards, each only able to be used once, and a class that handles
all the cards and finding a card to deal. However, I am receiving a
NullPointerException somewhere in the class handling the card dealing.
Here's the code: (Also, I am trying to get creative and do this
differently than the traditional way, such as using arrays or something.
:))
import java.util.Random;
public class Deal {
private static Random random = new Random();
private static int used1 = 0; //Ace
private static int used2 = 0; //2's
private static int used3 = 0; //3's
private static int used4 = 0; //4's
private static int used5 = 0; //5's
private static int used6 = 0; //6's
private static int used7 = 0; //7's
private static int used8 = 0; //8's
private static int used9 = 0; //9's
private static int used10 = 0; //10's
private static int used11 = 0; //Jacks's
private static int used12 = 0; //Queens's
private static int used13 = 0; //Kings's
private boolean validCardFound = false;
private static int card = 0;
private int findCard() {
    while(!validCardFound) {
        System.out.println("Test: Am I Run?"); //Nope, doesn't get run
        card = random.nextInt(13);
        testCard();
        }
    return card;
}
private boolean testCard() {
    boolean isAvailable = false;
    if(card == 1 && used1 != 4) {
        used1++;
        isAvailable = true;
    } else
    if(card == 2 && used2 != 4) {
        used2++;
        isAvailable = true;
    } else
    if(card == 3 && used3 != 4) {
        used3++;
        isAvailable = true;
    } else
    if(card == 4 && used4 != 4) {
        used4++;
        isAvailable = true;
    }  else
    if(card == 5 && used5 != 4) {
        used5++;
        isAvailable = true;
    }  else
    if(card == 6 && used6 != 4) {
        used6++;
        isAvailable = true;
    }  else
    if(card == 7 && used7 != 4) {
        used7++;
        isAvailable = true;
    }  else
    if(card == 8 && used8 != 4) {
        used8++;
        isAvailable = true;
    } else
    if(card == 9 && used9 != 4) {
        used9++;
        isAvailable = true;
    } else
    if(card == 10 && used10 != 4) {
        used10++;
        isAvailable = true;
    } else
    if(card == 11 && used11 != 4) {
        used11++;
        isAvailable = true;
    } else
    if(card == 12 && used12 != 4) {
        used12++;
        isAvailable = true;
    } else
    if(card == 13 && used13 != 4) {
        used13++;
        isAvailable = true;
    } else isAvailable = false;
    validCardFound = isAvailable;
    return isAvailable;
}
public int getCard() {
    System.out.println(card);
    validCardFound = false;
    return findCard();
}
}
     //------------------------------
 import java.awt.Color;
 import java.awt.Font;
 import java.awt.GridLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.SwingConstants;
  public class Screen {
private static Deal dealer;
private static final JFrame frame = new JFrame("BlackJack v1.0.0");
private static JPanel pane = new JPanel();
private static JButton startGame = new JButton("Start");
private static JButton viewRecord = new JButton("View Record");
private static JButton gameType = new JButton("Game Type");
private static JButton exit = new JButton("Exit");
private static JButton fold = new JButton("Fold");
private static JButton hit = new JButton("Hit");
private static JButton bet = new JButton("Bet");
private static final JLabel TITLE = new JLabel("BlackJack v1.0.0");
private static JLabel money = new JLabel();
public static int cardRecieved;
public static void MainScreen() {
    frame.setSize(350, 350);
    pane.setLayout(new GridLayout(5, 1));
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pane.setBackground(Color.DARK_GRAY);
    pane.setForeground(Color.WHITE);
    TITLE.setForeground(Color.WHITE);
    TITLE.setFont(new Font("Verdana", Font.PLAIN, 30));
    TITLE.setHorizontalAlignment(SwingConstants.CENTER);
    startGame.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            frame.dispose();
            startNewGame();
        }
    });
    exit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);
        }
    });
    frame.add(pane);
    pane.add(TITLE);
    pane.add(startGame);
    pane.add(viewRecord);
    pane.add(gameType);
    pane.add(exit);
}
public static void startNewGame() {
    cardRecieved = dealer.getCard();
    System.out.println(cardRecieved);
}
}
As you can tell, I kind of started screwing around and changing things to
see if I could fix it, but to no avail. All this gets executed after
hitting a JButton which then calls the getCard() method, but that's all
from another class.
Also I am still kinda new to Java, so sorry if I thought of a terrible way
to do this. It was the best I could think of.
Here is the StackTrace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Screen.startNewGame(Screen.java:71)
at Screen$1.actionPerformed(Screen.java:50)
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)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
 
No comments:
Post a Comment