0

I realize there are already a bunch of questions like this but every one I have seen so far the problem was the ArrayList was not initialized. I think mine is initialized but I get a NullPointer when trying to add to the list. Here is my code:

import java.util.ArrayList;
import java.util.Random;
import java.util.Collections;

public class Deck {
    static final int DECKSIZE = 48;
    static final int MAX = 10;
    static final int MIN = 1;
    public ArrayList<Card> cardDeck = new ArrayList<Card>();
    Random rand;

    public Deck() {
        initializeDeck();
    }

    private void initializeDeck() {
        for (int i=0; i<DECKSIZE; i++) {
            cardDeck.add(i, new Card("Name" + (i+1),  "Race" + (i+1), "Orientation" + (i+1)));
        }
    }

    public void shuffleCards() {
        Collections.shuffle(cardDeck);
    }
}

Here is stack trace telling me initializeDeck is a nullPointer

Caused by: java.lang.NullPointerException
    at hr.krypto.cardwars.deck.Deck.initializeDeck(Deck.java:24)
    at hr.krypto.cardwars.deck.Deck.<init>(Deck.java:15)
    at hr.krypto.cardwars.deck.Dealer.<init>(Dealer.java:14)
    at hr.krypto.cardwars.model.CardTable.<init>(CardTable.java:15)
    at hr.krypto.cardwars.screen.GameScreen.show(GameScreen.java:31)
    at com.badlogic.gdx.Game.setScreen(Game.java:62)
    at hr.krypto.cardwars.CardWarsGame.create(CardWarsGame.java:19)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:127)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110)
8
  • 3
    please post stacktrace Commented Mar 27, 2013 at 10:43
  • The stacktrace should indicate where you have a problem. Commented Mar 27, 2013 at 10:44
  • @Keyser this method can throw only IndexOutOfBoundsException Commented Mar 27, 2013 at 10:46
  • 1
    cardDeck.add(i - Don't add at an index position, just add (to the end) Commented Mar 27, 2013 at 10:49
  • 1
    <offtopic>Shouldn't there be 52 cards in the deck?</offtopic> Commented Mar 27, 2013 at 10:56

2 Answers 2

1

Considering that the method ArrayList.add(index, Element) only throws IndexOutOfBoundsException the error must be in the constructor of your card class.

Guess the fastest way would be to debug into the card class constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

@Henrik your right, but considering that it happends on the add methode, whcih has only an int and element parameter doesnt leave much place where an error can occure other that in the constructor of the new object. therefor just debugging.
0

Inside the constructor Card(String, String, String) a NullPointerException is raised. As shown by the stacktrace with line number.

If an other constructor Card() does some initialisation, simply first call this() inside the added constructor.


After the stack trace was added:

Correction. I bet in the ran code still rand is used, which is not initialized (null). Do a clean first before building.

Or there are two classes Deck in different packages, is the shown class in package hr.krypto.cardwars.deck;?

5 Comments

You posted the answer before a StackTrace was actually posted by the OP. Amazing!
@StefanBe you are right, I am turning into a puzzler. Here it is an unavoidable conclusion though. As the shown code is basically correct.
Both your answer and TwoMore's answer helped point me in the right direction. rand was not initialized
@tomislav2012 please show us the constructor I am so curious about the problem :)
no need to post Card class, i was populating Card class with rand values from Deck, and rand was not initialized. Code runs fine now. Thanks SO!!!!! you rock

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.