java - 没有明显代码错误的空指针异常错误

标签 java nullpointerexception

我这里有一个错误,我不知道它是从哪里来的。我在初学者的 java 类是高中,所以我在这里还没有太多经验。我有 3 个相互结合的程序。 我有一个创建扑克牌的卡片类

    //********************************************************************
//  Card.java       Author: Lewis and Loftus
//
//  Solution to Programming Project 4.5
//********************************************************************

import java.util.*;

public class Card
{
   public final static int ACE   = 1;
   public final static int TWO   = 2;
   public final static int THREE = 3;
   public final static int FOUR  = 4;
   public final static int FIVE  = 5;
   public final static int SIX   = 6;
   public final static int SEVEN = 7;
   public final static int EIGHT = 8;
   public final static int NINE  = 9;
   public final static int TEN   = 10;
   public final static int JACK  = 11;
   public final static int QUEEN = 12;
   public final static int KING  = 13;

   public final static int CLUBS    = 1;
   public final static int DIAMONDS = 2;
   public final static int HEARTS   = 3;
   public final static int SPADES   = 4;

   private final static int NUM_FACES = 13;
   private final static int NUM_SUITS = 4;

   private int face, suit;
   private String faceName, suitName;

   private int myInt1, myInt2;

   Random rand = new Random();

   //-----------------------------------------------------------------
   //  Creates a random card.
   //-----------------------------------------------------------------
   public Card ()
   {
      face = rand.nextInt(4) + 1;
      setFaceName();

      suit = rand.nextInt(13) + 1;
      setSuitName();
   }



   //-----------------------------------------------------------------
   //  Sets the string representation of the face using its stored
   //  numeric value.
   //-----------------------------------------------------------------
   private void setFaceName()
   {
      switch (face)
      {
         case 1:
            faceName = "Ace";
            break;
         case 2:
            faceName = "Two";
            break;
         case 3:
            faceName = "Three";
            break;
         case 4:
            faceName = "Four";
            break;
         case 5:
            faceName = "Five";
            break;
         case 6:
            faceName = "Six";
            break;
         case 7:
            faceName = "Seven";
            break;
         case 8:
            faceName = "Eight";
            break;
         case 9:
            faceName = "Nine";
            break;
         case 10:
            faceName = "Ten";
            break;
         case 11:
            faceName = "Jack";
            break;
         case 12:
            faceName = "Queen";
            break;
         case 13:
            faceName = "King";
            break;
      }
   }

   //-----------------------------------------------------------------
   //  Sets the string representation of the suit using its stored
   //  numeric value.
   //-----------------------------------------------------------------
   private void setSuitName()
   {
      switch (suit)
      {
         case 1:
            suitName = "Clubs";
            break;
         case 2:
            suitName = "Diamonds";
            break;
         case 3:
            suitName = "Hearts";
            break;
         case 4:
            suitName = "Spades";
            break;
      }
   }

   //-----------------------------------------------------------------
   //  Determines if this card is higher than the passed card. The
   //  second parameter determines if aces should be considered high
   //  (beats a King) or low (lowest of all faces).  Uses the suit
   //  if both cards have the same face.
   //-----------------------------------------------------------------
   public boolean isHigherThan (Card card2, boolean aceHigh)
   {
      boolean result = false;

      if (face == card2.getFace())
      {
         if (suit > card2.getSuit())
            result = true;
      }
      else
      {
         if (aceHigh && face == ACE)
            result = true;
         else
            if (face > card2.getFace())
               result = true;
      }

      return result;
   }

   //-----------------------------------------------------------------
   //  Determines if this card is higher than the passed card,
   //  assuming that aces should be considered high.
   //-----------------------------------------------------------------
   public boolean isHigherThan (Card card2)
   {
      return isHigherThan (card2, true);
   }

   //-----------------------------------------------------------------
   //  Returns the face (numeric value) of this card.
   //-----------------------------------------------------------------
   public int getFace ()
   {
      return face;
   }

   //-----------------------------------------------------------------
   //  Returns the suit (numeric value) of this card.
   //-----------------------------------------------------------------
   public int getSuit ()
   {
      return suit;
   }

   //-----------------------------------------------------------------
   //  Returns the face (string value) of this card.
   //-----------------------------------------------------------------
   public String getFaceName ()
   {
      return faceName;
   }

   //-----------------------------------------------------------------
   //  Returns the suit (string value) of this card.
   //-----------------------------------------------------------------
   public String getSuitName ()
   {
      return suitName;
   }

   //-----------------------------------------------------------------
   //  Returns the string representation of this card, including
   //  both face and suit.
   //-----------------------------------------------------------------



   public String toString ()
   {
      return faceName + " of " + suitName;
   }
}

我有一个可以创建 52 张卡片的 Deck Of cards 文件

import java.util.*;


public class DeckOfCards 
{

    private Card deckOfCards[];
    private int currentCardUsed;
    private final int NumberOfCards = 52;

    private int nextCard;

    private Random rand;

    String myString = "All Cards have been dealt.";




    public DeckOfCards()
    {
        deckOfCards = new Card[NumberOfCards];
        currentCardUsed = 0;

        Random rand = new Random();

        for(int index = 0; index < deckOfCards.length; index ++)
        {
            deckOfCards[index] = new Card();
        }
    }

    public void shuffleCards()
    {
            currentCardUsed = 0;

            for(int newCard = 0; newCard < deckOfCards.length; newCard ++)
            {
                int nextCard = rand.nextInt(NumberOfCards);

                Card temporaryDeck = deckOfCards[newCard];
                deckOfCards[newCard] = deckOfCards[nextCard];
                deckOfCards[nextCard] = temporaryDeck;
            }
    }

    public Card dealCard()
    {
        if(currentCardUsed < deckOfCards.length)
        {
            return deckOfCards[currentCardUsed ++];
        }
        else
        {
            return null;
        }
    }
}

最后我有了一个驱动类

public class DeckTester 
{
    public static void main(String [] args)
    {
        DeckOfCards deck = new DeckOfCards();

        deck.shuffleCards();

        System.out.println(deck.dealCard());
        System.out.println(deck.dealCard());

        System.out.println(deck.dealCard());
        System.out.println(deck.dealCard());
        System.out.println(deck.dealCard());
        System.out.println(deck.dealCard());

    }
}

它们都没有错误。但是当我运行驱动程序时,我得到了这个输出

Exception in thread "main" java.lang.NullPointerException
    at DeckOfCards.shuffleCards(DeckOfCards.java:39)
    at DeckTester.main(DeckTester.java:8)

我曾尝试更改交易方法中的 Null,但无济于事。

最佳答案

看起来您正在构造函数中声明一个本地 Random 对象。

尝试将第 25 行更改为:

 rand = new Random();

关于java - 没有明显代码错误的空指针异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33771682/

相关文章:

java - 访问 fragment 内的 TextView 会给出 NullPointerException

java - Android onCreate 中的 NullPointerException

java - jGRASP 支持 Java lambda 表达式吗?

java - 使用 SecureRandom 时线程 "AWT-EventQueue-0"java.lang.NullPointerException 中出现异常

java - 什么是NullPointerException,我该如何解决?

java - 我无法配置个人适配器

java - 捕获空指针异常

macos - Mac OS X 中的 JDK

java - 具有接口(interface)参数的反射和构造函数

Java 相当于 WCF 数据服务