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

2015/12/16

Null Pointer Exception

Chapaladeen Chapaladeen

2015/12/16

#
Hey there, I've been having a really irritating null pointer exception that I've been trying to solve for about an hour.
static public void cardListText() 
    {
        int holder;
      >> Should initialize everything  Game.Game();
       >>Causes Null Pointer Exception if (Game.cardArray[gameWorld.listCards][Characters.ActivePlayer] == 1){
            unknown++;
            holder = unknown * 20;
            coordY = unknown + 200;
        }
    } 
And here is the area that apparently doesn't run properly when called.
static public void Game() 
    {
        //Uses Initialise variable as a flag for only running initialise once as a subroutine.
       if (Initialise == 1){
           initialise();
     }
    }

    static public void initialise(){
        // This subroutine initialises the variables that are going to be public.
        cardArray = new int[21][7];
        for (int i = 0; i < 7; i++){
            for (int x = 0; x <= 20; x++){
                //Sets how many spaces are in the array and sets the values of all the places 
                //in cardArray to 0.
                cardArray[x][i] = 0;
                System.out.println(cardArray[x][i]);
            }
        }
        cardDistribution();
    }

    static public void cardDistribution() 
    {
        //This procedure is to distribute the cards evenly. It will get 3 murder cards, one from each section and then it will assign the 18
        //remaining cards evenly to each player. I am passing in cardArray as it is a global variable due to how important it is.
        
        // I will need Rand to hold random values that will be used to decide what card a person gets. Count is used to compare
        //to run a while loop for the amount of players in the game (e.g. 3 players = 3 loops). countCard will be used later in a
        //WHILE loop. The final int is going to be used to work out the amount of cards each player has.
        
        
        int Count = 1,impossible = 0, countCard =0, playercardCount, rand, playerCount = 6; //activePlayer, cardArray and playercount need to be deleted once implemented globally.
       
        //In this section of code I generate 3 random numbers. The first between 1 and six; the second between 7 and 12 and the
        //third between 13 and 21. This is because the first 6 cards are the cards for people; the next six are the cards for
        //weapons and then from 13 and onwards to 21 are rooms.
        rand = ThreadLocalRandom.current().nextInt(1, 6 + 1);
        cardArray[rand][0] = 7;
        rand = ThreadLocalRandom.current().nextInt(7, 12 + 1);
        cardArray[rand][0] = 7;
        rand = ThreadLocalRandom.current().nextInt(13, 21 + 1);
        cardArray[rand][0] = 7;
        
        // This WHILE loop runs while countCard is less than or equal to 18. This is because 3 cards have been assigned as the
        //murder cards, so out of 21 cards there are only 18 left to assign.
        while (countCard <= 18){
            playercardCount = 0;
            
            while (Characters.ActivePlayer <= playerCount){
                
                if (cardArray[rand][6] == 0){    
                    //Assigns the card to slot 7 as the value of the active player. Slot 7 holds all of the
                    //values for lookup later by other players.
                    cardArray[rand][0] = Characters.ActivePlayer;;
                    //Assigns the card to the player’s own slot in the cardArray. This is cards that they
                    //know are in play and will be displayed to them in the game.
                    cardArray[rand][Characters.ActivePlayer] = Characters.ActivePlayer;
                    
                    //Increments the loop of countCard so that no more than 18 cards are assigned.
                    countCard++;
                    
                    //Increments the playercardCount.
                    playercardCount++;

                    
                    //Makes sure that the player is not assigned more cards than they need.
                    if (playercardCount == 18/playerCount){
                    
                        //Increments the Count to go to and start assigning cards to the next player.
                        Characters.ActivePlayer ++;
                        
                }
             }
         }
      }
      Initialise = 0;                
    }
Just for reference, the Game.Game(); part should not be where it is, I just put it there as I thought it may be that it was not getting initialized before running that, but apparently not. it also wont compile at all and the exception happens on attempt to compile.
danpost danpost

2015/12/16

#
You have other references in the line that could possibly be null -- 'gameWorld' and, 'Characters'. Now, you can check each one with the following inserted before where the error occurs:
System.out.println("gameWorld = "+gameWorld);
System.out.println("Characters = "+Characters);
to see what may be 'null'. Then you can trace back to see why it may be 'null'.
You need to login to post a reply.