简体   繁体   English

Java:初始化一个新的LinkedList集合

[英]Java: Initializing a new LinkedList Collection

I am trying to build a simple card game as a personal exercise. 我正在尝试建立一个简单的纸牌游戏作为个人练习。 I have a collection Cards that should contain my deck. 我有一张收藏Cards ,应该包含我的套牌。 To initialize it, I want to pass it a map of what the deck should look like - an integer array (1 to n, 1 to 2) with (n, 1) containing a card type which is resolved within the card class, and (n, 2) containing the number of cards that type I want in the deck. 为了初始化它,我想传递一个甲板应该是什么样子的地图 - 一个整数数组(1到n,1到2),其中(n,1)包含在卡类中解析的卡类型,以及(n,2)包含我在牌组中想要的牌数。 I'm having difficulties with a NullPointer exception, however. 但是,我遇到了NullPointer异常的困难。 Here is my Cards class: 这是我的Cards类:

import java.util.LinkedList;

public class Cards{
    private LinkedList<Card> CardDeck;

  ...

    public boolean MakeDeck(int[][] DeckMap){
        /*feed the function a 2D int array (0 to n, 0 to 1)

        @Param - DeckMap[][] - [n][0] to contain card type
        [n][1] to contain DupeCount*/

        //search the array for duplicates
        for (int i = 0; i < DeckMap.length; i++){
            int hold = DeckMap[i][0];
            DeckMap[i][0] = -10;
            for (int j = 0; j< DeckMap.length; j++){        
                if (DeckMap[j][0] == hold){
                    DeckMap[i][0] = hold;
                    return false;
                }       
            }
            DeckMap[i][0] = hold;
        }

        //Add the cards
        // tried variations on this: CardDeck = new LinkedList<Card>;
        for (int i = 0; i< DeckMap.length; i++){
            Card cC = new Card();
            cC.initializeCard(DeckMap[i][0], DeckMap[i][1]);
            CardDeck.addLast(cC);
        }
        return true;


    }
}

The NullPointer error occurs at the cC.addLast line - since I have initialized the Card class, the Null Pointer should refer to the CardDeck LinkedList I want to add the Card to, I think. NullPointer错误发生在cC.addLast行 - 因为我已经初始化了Card类,Null指针应该引用我想要添加CardCardDeck LinkedList,我想。 But I can't work out how to initialize the list. 但我无法弄清楚如何初始化列表。 Or is the .initializeCard call the problem (code below)? 或者.initializeCard调用问题(下面的代码)? Thanks in advance for your help and apologies if I've missed something obvious. 如果我错过了一些明显的东西,请提前感谢您的帮助和道歉。

Error: 错误:

java.lang.NullPointerException at towergame.Cards.MakeDeck(Cards.java:75) towergame.Cards.MakeDeck(Cards.java:75)中的java.lang.NullPointerException

  public class Card {

        private static String cName;
        private static int cDuplicateCount;
        public static cEffect myEffects;

        public final void initializeCard(int inEffect, int DupeCount){
            myEffects = new cEffect();
            myEffects.setEffect(inEffect);
            cName = myEffects.getCardType();
            cDuplicateCount = DupeCount;
        }
     ...
   }

Instead of this private LinkedList<Card> CardDeck; 而不是这个private LinkedList<Card> CardDeck;

use this private LinkedList<Card> CardDeck = new LinkedList<Card>(); 使用此private LinkedList<Card> CardDeck = new LinkedList<Card>();

it is throwing NPE because cardDeck has not been initialized. 它正在抛出NPE,因为cardDeck尚未初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM