Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: nullpointerexception error

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default nullpointerexception error

    I'm writing a java video poker game. When I run the test file, I get a nullpointexception error. It points to this line in my shuffling method
     theDeck[i] = theDeck[j];
    I'm still a novice in java...

     import java.util.Random;
    public class Deck {
     
    private Card[] theDeck; 
    private int top; 
    // add more instance variables if needed
     
    public Deck(){
        top = 0;
        Card[] theDeck = new Card[52];
        for(int s = 1; s <= 4; s++)
        {
            for (int v = 1; v <= 13; v++)
            {
                for (int i = 0; i < theDeck.length; i++)
                {
     
                    theDeck[i] = new Card(s,v);
                }
            }
        }   
    }
     
    public void shuffle()
    {
        // shuffle the deck here
        Random generator = new Random();
        int i;
        int j;
        i = generator.nextInt(51) + 1;
        j = generator.nextInt(51) + 1;
        Card temp = theDeck[i];
        for(int k = 1; k <100; k++)
        {
            theDeck[i] = theDeck[j];
            theDeck[j] = temp;
        }
        top = 0;
    }

    this is the part of the class that calls the shuffle method:

     public void play()
    {
        cards.shuffle();
        for( int i =1; i <= 5; i++)
        {
            p.addCard(cards.deal());
            cards.incrementTop();
        }
        for( int j = 1; j < 5; j++)
        {
            p.getHand().get(j).toString();
        }   
        System.out.println("These are your cards:");
        for( Card c : p.getHand())
        {


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: nullpointerexception error

    you have this instance private Card[] theDeck;
    that statement is similar to private Card[] theDeck = null; a null pointer.
    array in java is an Object, and if not equate to array object or make a new object, it would be null. and when you attempt to read of write in an object that points to null, it will make a NullPointerException.

    it throws a NullPointerException in your method shuffle(), since it attempted to write in that array

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: nullpointerexception error

    I fixed this error, and now I get another one in these two classes:

     import java.util.Collections;
    public class Player 
    {
     
     
    	private ArrayList<Card> hand; // the player's cards
    	// you will likely need more instance variables
     
    	public Player()
    	{		
    		ArrayList<Card> hand = new ArrayList<Card>();
    	}
     
    	public void addCard(Card c)
    	{
    		hand.add(c);
    		// add the card c to the player's hand
    	}
     
    	public void removeCard(Card c)
    	{
    		hand.remove(c);
    		// remove the card c from the player's hand
    	}
     
    	public ArrayList<Card> getHand()
    	{
    		return hand;
    	}
     
    	public void sortHand()
    	{
    		Collections.sort(hand);
    	}	
    	//public void printHand()
    	//{
     
    	// you will likely need more methods here
    	//DB such as methods for analysing the hand 7
    	//and the sorting method.
    }

    import java.util.Random;
    public class Deck {
     
    	private Card[] theDeck; 
    	private int top; 
    	// add more instance variables if needed
     
    	public Deck(){
    		theDeck = new Card[52];
    		for(int s = 1; s <= 4; s++)
    		{
    			for (int v = 1; v <= 13; v++)
    			{
    				for (int i = 0; i < theDeck.length; i++)
    				{
     
    					theDeck[i] = new Card(s,v);
    				}
    			}
    		}	
    	}
     
    	public void shuffle()
    	{
    		// shuffle the deck here
    		Random generator = new Random();
    		int i;
    		int j;
    		for(int k = 1; k <100; k++)
    		{
    			i = generator.nextInt(51) + 1;
    			j = generator.nextInt(51) + 1;
    			Card temp = theDeck[i];
    			theDeck[i] = theDeck[j];
    			theDeck[j] = temp;
    		}
    		top = 0;
    	}
     
    	public Card deal()
    	{
    		// deal the top card in the deck
    		return theDeck[top];
    	}	
     
    	public void incrementTop()
    	{
    		top = top + 1;
    	}	
     
     
     
     
     
     
    	// add more methods here if needed
    	//DB getter methods here?
     
    }

    this is the error I get:

     $ javac Deck.java
    $ java PokerTest
    Exception in thread "main" java.lang.NullPointerException
            at Player.addCard(Player.java:17)
            at Game.play(Game.java:71)
            at PokerTest.main(PokerTest.java:9)

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: nullpointerexception error

    I believe that the error is in you main method. in PokerTest.java

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: nullpointerexception error

    Any idea why the addCard() method returns a null?

  6. #6
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: nullpointerexception error

    because it points to a null. instance hand and local variable hand in your constructor are not the same. please check

  7. The Following User Says Thank You to dicdic For This Useful Post:

    denbal87 (April 6th, 2014)

  8. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: nullpointerexception error

    Hmm I doubt it; I think the error must be in the deck class. All I have in the tester class is this:

     Game g = new Game();
                            g.play();


    --- Update ---

    You're right! Thanks!

  9. #8
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    284
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: nullpointerexception error

    believe me that is the error.
    public class Player {
     
          private ArrayList<Card> hand; // points to null
     
          public Player {
                ArrayList<Card> hand = new ArrayList<Card>(); // instance variable still points to null
                // hand here is a local variable
          }
     
          public void addCard(Card card) {
                hand.add(card);
          }
    }

Similar Threads

  1. NullPointerException Error
    By TaoNinja in forum Exceptions
    Replies: 6
    Last Post: December 21st, 2012, 03:43 PM
  2. Error, NullPointerException
    By alex067 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: October 14th, 2012, 09:05 AM
  3. NullPointerException Error
    By nicsa in forum Exceptions
    Replies: 6
    Last Post: November 19th, 2011, 04:32 PM
  4. NullPointerException error
    By pyrotecc25 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 14th, 2011, 01:17 PM
  5. NullPointerException error
    By blazerix in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 24th, 2011, 08:59 PM

Tags for this Thread