0

I am working on a GoFish game in java in my take turn method I wrote I receive a Null Exception Error, and I know that means I have a null object.

When going through my code I can not figure out why I am getting this. When I ask the user for number of player it creates that number. SO why would I get a null error for this

if((P.cardsInHand[P.getnumCardsInHand()].getRank()) == rank){
                    return P;
                }


private Player[] numPlayers;
int playerName;
int requestedcard;
public void numPlayers(){
        Scanner input = new Scanner(System.in);
        System.out.println("How many players want to play");
        numPlayer = input.nextInt();

        if((numPlayer < 2) || (numPlayer > 6)){
            System.out.println("How many players want to play");
            numPlayer = input.nextInt();
        }
        numPlayers = new Player[numPlayer];

        for (int i = 0; i <  numPlayer; i++){

            System.out.println("Player Number(Start with 0)");
            playerName = input.nextInt();
            numPlayers[i] = new Player(playerName);
        }
        requestedPlayer = numPlayers[0];
    }

     public Player taketurn(Player P){
         previousPlayer = P;
         System.out.println(P);
        //set up scanner
        Scanner input = new Scanner(System.in);
        //declare variables
        int player;
        int rank;
        int numMatches = 0;
        System.out.println("Player " + previousPlayer.getnumplayerNumber() + " turn");
        //Player P ask what player cars it want to sk for
        System.out.println("what Player Do you want to ask");
        player = input.nextInt();
        if(player > numPlayer){
            System.out.println("what Player Do you want to ask");
            player = input.nextInt();
        }
        //data type of Player is set to the player that Play P requested
        requestedPlayer = numPlayers[player];
        //request card
        System.out.println("What rank do you want(2-10 = the corresponding number rank on the card, 11 = jack, 12 = queen, 13 = king, 14 = ace)");
        rank = input.nextInt();


        for(int i = 1; i < (requestedPlayer.getnumCardsInHand()); i++){
            if((requestedPlayer.cardsInHand[i].getRank()) == rank){
                // if requested rank == to the player that was aksed for the card
                //the card will be transfered and removed from hand
                requestedPlayer.cardsInHand[i].test1();
                System.out.println("Card of rank " + rank + " are being transfered");
                //requestedPlayer.cardsInHand[i].test();
                for(int j = 1; j < (requestedPlayer.getnumCardsInHand()); j++){
                    P.transfercard(i, requestedPlayer);
                    requestedPlayer.removeCard(i);
                }


                P.findBooks();
                return P;
            }else{
                //place draw in paremters
                System.out.println("Go Fish");
                P.addCard(d.draw());
                if((P.cardsInHand[P.getnumCardsInHand()].getRank()) == rank){
                    return P;
                }else{
                    System.out.println("Requested Players turn");
                    return (requestedPlayer);
                }
            }
        }  
        return requestedPlayer;
    }
jmurphy
  • 23
  • 5
  • 2
    Please more information. Like the line where the exception occurs. – deviantfan Mar 07 '14 at 20:20
  • It happens at this point if((P.cardsInHand[P.getnumCardsInHand()].getRank()) == rank){ return P; } It is line 88 in the program I am using to write the code. Can you attach documents it might be easier to explain if i could attach the player class too. – jmurphy Mar 07 '14 at 20:21
  • then either `P` is null or `P.cardsInHand[P.getnumCardsInHand()]` returned null. ..or `P.getnumCardsInHand()` returned null – donfuxx Mar 07 '14 at 20:23
  • When the exception is thrown, what is the value of `P.getnumCardsInHand()`? How many elements are in `P.cardsInHand[]`? – David Mar 07 '14 at 20:24
  • Which method call taketurn method ?? – Kick Mar 07 '14 at 20:24
  • Use breakpoints to view variables during execution. You should be able to identify exactly what the issue is then. – WOUNDEDStevenJones Mar 07 '14 at 20:24
  • The program that I used to write this program does not have break points ability. The P.cardsInHand holds 25 items but when the game starts only the first five 5 spaces are filled with card objects, and the other 20 is space for more card to be added during the game. – jmurphy Mar 07 '14 at 20:29
  • The reference P of class Player in the parameter of method taketurn is null – Kick Mar 07 '14 at 20:29
  • possible duplicate of [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – Brian Roach Mar 07 '14 at 20:31
  • How is the parameter null? When I call it with requestedPlayer in the numPlayer method it takes the value of the first player which has been initialized. – jmurphy Mar 07 '14 at 20:32
  • It's almost certain that `P.cardsInHand[nnn]` is `null`, where `nnn` is the array element you're trying to access. If you said `cardsInHand = new Card[5]` or something, but forgot to set each element to a `Card`, they will all be `null`. Otherwise, maybe you initialized some of the elements but didn't initialize all of them. – ajb Mar 07 '14 at 20:39
  • On the line before you receive a NPE, add statements like `if (P == null) { System.out.println("P is null") }` to quickly determine which object is null, then work backwards. – turbo Mar 07 '14 at 20:40
  • What is `getnumCardsInHand()`? If it's the actual number of cards in a player's hand, please remember that array indexing starts at 0, so if `N` items in your array are used, they will be indexed from 0 to `N-1`, and in that case the `[N]` element will not exist (and might be `null` to mean an unused element). – ajb Mar 07 '14 at 20:41
  • @turbo keep in mind that if `P` were null, the exception would have been thrown on the previous line. – ajb Mar 07 '14 at 20:42
  • @ajb ah true, I just picked that for ease of writing, my mistake. – turbo Mar 07 '14 at 20:48

1 Answers1

0

If P.cardsInHand is an array with 25 elements, but you're only using the first 5, then the elements P.cardsInHand[0], P.cardsInHand[1], ... P.cardsInHand[4], will have values, but P.cardsInHand[5] will probably be null. If getnumCardsInHand() returns 5, then the array element will be null, which will cause the NullPointerException. Use P.getnumCardsInHand()-1.

Since arrays in Java (and C and many other languages) are indexed starting at 0, that means that if you want to access the last element of the array (or, in your case, the last "used" element), you need to subtract 1 from any value that gives you the number of elements (such as a.length for an array, a.size() for an ArrayList, or in your case P.getnumCardsInHand()).

ajb
  • 31,309
  • 3
  • 58
  • 84