This site requires JavaScript, please enable it in your browser!
Greenfoot back
kdbailey
kdbailey wrote ...

2015/3/5

Need help with an array

kdbailey kdbailey

2015/3/5

#
So I'm trying to make a program that shuffles cards by storing their information and randomly moving them. All the information and programming is set and it compiles, however whenever I try to load the array, I get a java.lang.NullPointerException. I've been trying to figure out what the problem is but I need some help This is the World
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class CardWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CardWorld extends World
{

    /**
     * Constructor for objects of class CardWorld.
     * 
     */
    public CardWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        
    }
      Cards[] thisDeck;
    /**
     * Act - do whatever the Cards wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.isKeyDown("l"))loadDeck(thisDeck);
        if (Greenfoot.isKeyDown("s"))showCards(thisDeck);
    }    

    public void loadDeck(Cards[] thisDeck)
    {
        String cardName;
        GreenfootImage cardPic;
        int suit;
        int cardVal;
        int cardRank;
        Cards thisCard;
        for (int suitCount = 0; suitCount <=3; suitCount++)
        {
            for (int count = 101; count <= 113; count++)
            {
                cardName = (suitCount)*13 + count + ".gif";
                cardPic = new GreenfootImage(cardName);
                suit = suitCount;
                cardRank = count -100;
                if (cardRank == 1) cardRank = 14;
                if (count < 111)
                {
                    cardVal = count -100;
                }
                else
                {
                    cardVal = 10;

                }
                int index = (count -100)+ 13*(suitCount);
                thisCard = new Cards(cardPic, suit, cardVal, cardRank, index);
                thisDeck[index] = thisCard;             
            } 
           
        }
    }
    
    public void showCards(Cards[] thisDeck)
    {
        Cards thisCard;
        int index;
        //GreenfootImage thisImage;
        for (int suit = 0; suit <=3; suit++)
        {
            for (int counter = 1; counter <= 13; counter++)
            {
                index = counter + suit * 13;
                thisCard = thisDeck[index];
                addObject(thisCard, 50 + counter*20, 50+50*suit);  //space out the cards on the screen
            }
        }
    }
}
this is the Cards itself
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Cards here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cards extends Actor
{
    public GreenfootImage cardFace;
    public int suit;
    public int faceValue;
    public int cardRank;
    public int index;
       public Cards(GreenfootImage parFace, int parSuit, int parValue, int parRank, int parIndex)
    {
     cardFace = parFace;
     setImage(cardFace);
     suit = parSuit;
     faceValue = parValue;
     cardRank = parRank;
     index = parIndex;
    } 

   
    /**
     * Act - do whatever the Cards wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {

    }    



    
}
any help would be greatly appreciated
davmac davmac

2015/3/5

#
Line 22:
      Cards[] thisDeck;
This creates a variable which can hold a reference to an array, but it doesn't create an array. Nowhere in your code is an array created, so there is no array; 'thisDeck' remains null. When you try to use it, you would get a NullPointerException. You need to initialise the variable with an array:
      Cards[] thisDeck = new Cards[52];
(By the way, if you need help with an exception, please post the stack trace or at least say which line you get the exception on! This makes it much easier to diagnose.)
You need to login to post a reply.