Results 1 to 6 of 6
  1. #1
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default Null pointer exception a

    Here's the code. The problem first occurs at "deck[Counter].setSuit(CHDS);"

    Java Code:
    import java.util.Scanner;
    
    public class Game {
    
        public static void main(String[] args){
    
            Game game=new Game();
            game.playGame();
    
        }
    
        private int numPlayers;
        private Card[] deck = new Card[51];
        private Card.suits trumps;
        private Player players[]= new Player[4];
        Scanner input=new Scanner(System.in);
        private int Counter=0;
        
        public Game(){
    
           // Initialize deck
           for(Card.suits CHDS : Card.suits.values()){
                
                for(int n=1;n<14;n++){
    
                    deck[Counter].setSuit(CHDS);
                    deck[Counter].setValue(n);
                    Counter++;
    
                }
    
            }
    
            // Sets trumps
            trumps=Card.suits.C;
            System.out.println("Trumps are clubs.");
    
            // Code to get user to input number of players
            System.out.println("Enter the desired number of players");
            numPlayers=input.nextInt();
    
        }
    
        public void playGame(){
    
        }
    
    }
    And the Card class:

    Java Code:
    import java.util.Random;
    
    public class Card {
    
        private Random rand=new Random();
        private suits suit;
        private int value;
    
        public enum suits{C, H, D, S;}
    
        public Card(suits theSuit, int theValue){
            suit=theSuit;
            value=theValue;
        }
    
        public void dealHand(){
    
        }
    
        public Card[] shuffleDeck(Card theDeck[]){
            Card tempDeck[]= new Card[51];
            for(int n=0;n<51;n++){
                rand.nextInt(52);
            }
            return tempDeck;
        }
    
        public suits getSuit() {
            return suit;
        }
    
        public void setSuit(suits suit) {
            this.suit = suit;
        }
    
        public int getValue() {
            return value;
        }
    
        public void setValue(int value) {
            this.value = value;
        }
    
    }

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Java Code:
    for(int n=1;n<14;n++)
    This looks like your problem. Remember that Java starts counting at 0, so 13 will be out of range.

  3. #3
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Iron Lion View Post
    Java Code:
    for(int n=1;n<14;n++)
    This looks like your problem. Remember that Java starts counting at 0, so 13 will be out of range.
    Thanks for the reply. I thought that in the case of that particular for loop java would start counting at 1 continue to 13 and then stop.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    Its not the number of times thru the loop, its that the index goes past the end of the array.
    for(int i=12; i < arraySize; i++)
    i can start anywhere. It does not have to start at 0. Yet the array's first element is at index 0. This code would skip the elements in the first part of the array.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    It is not the loop that is the problem. It is the fact that the array is full of nulls and not Card objects as they are never created. Thus when trying to call the setSuit method on a null reference causes the NPE.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    Whoops. Ran off at the mouth without having the OP post the Full text of the error message.

    OP - Please post the full text of the error message when you get errors. It saves a lot of false starts.

Similar Threads

  1. Null Pointer Exception HELP!?
    By 2wyked in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 01:41 AM
  2. Null Pointer exception (Again !!)
    By mobosecomin in forum New To Java
    Replies: 6
    Last Post: 03-29-2011, 05:04 PM
  3. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 03:22 AM
  4. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •