Results 1 to 13 of 13
  1. #1
    dylandrop is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default null pointer exception problem

    Having problem with a blackjack game I want to make...
    Exception in thread "main" java.lang.NullPointerException
    at Player.evaluate(Player.java:29)
    at Player.hit(Player.java:18)
    at Game.play(Game.java:23)
    at Test.main(Test.java:6)

    It's weird because when I access the Player's hand, it says it's length 2. How can there be a null pointer exception when there's a card there??
    Here's the "problematic" Player class...

    Java Code:
    import java.util.ArrayList;
    public class Player {
    	
    	private ArrayList<Card> hand;
    	private int runningTotal;
    	
    	public Player(){
    		hand=new ArrayList<Card>();
    		runningTotal=0;
    	}
    	
    	public void addCard(Card c){
    		hand.add(c);
    	}
    	
    	public boolean hit(){
    		boolean isBust=false;
    		evaluate();
    		if(runningTotal>21)
    			isBust=true;
    		return isBust;
    	}
    	
    	public void evaluate(){
    		int blackJValue;
    		int cardValue;
    		for(int i = (hand.size() - 1); i>0; i--){
    			Card c = hand.get(i);
    			System.out.println(c.toString());
    			cardValue=c.value();
    			if(cardValue>10){
    				blackJValue=10;
    			}
    			else if(cardValue==1){
    				blackJValue=1;
    			}
    			else{
    				blackJValue=cardValue;
    			}
    			runningTotal+=blackJValue;
    		}
    	}
    	
    	public int getScore(){
    		return runningTotal;
    	}
    	
    	public int getHandLength(){
    		return hand.size();
    	}
    	
    }
    Last edited by Fubarable; 04-02-2011 at 07:49 PM. Reason: code tags added

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    A null pointer exception is thrown when an application attempts to use a null value in a case where an object is required. In most cases the object hasn't been initialised.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Welcome to the Java-forums!

    Which line causes the NPE? Can we see the code that uses Player?

    Also, I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck, and again, welcome!

  4. #4
    dylandrop is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    Sure. Here's the Game class

    Java Code:
    import java.util.Scanner;
    public class Game {
    
    Scanner blackJ = new Scanner(System.in);
    private Deck d;
    private Dealer p1;
    private Player p2;
    
    public Game(){
    	d=new Deck();
    	p1=new Dealer();
    	p2=new Player();
    }
    
    public void play(){
    	d.shuffle();
    	p1.addCard(d.dealCard());
    	p1.addCard(d.dealCard());
    	p2.addCard(d.dealCard());
    	p2.addCard(d.dealCard());
    	System.out.println(p2.getHandLength());
    	int choice;
    	while(p2.hit())
    		choice=0;
    		System.out.println("Hit? 1 - yes, 0 - no: ");
    		choice=blackJ.nextInt();
    		if(choice==1)
    			p2.addCard(d.dealCard());
    	while(p1.hit()){
    		while(p1.getScore()<17)
    			p1.addCard(d.dealCard());
    	}
    	getScore();
    }
    
    private void getScore(){
    	int scorep1 = p1.getScore();
    	int scorep2 = p2.getScore();
    	if(scorep1>scorep2 && scorep1<=21)
    		System.out.println("You win this round.");
    	else if(scorep1==scorep2 && scorep1<=21)
    		System.out.println("It's a push.");
    	else if(scorep1<scorep2 && scorep2<=21)
    		System.out.println("You lose this round.");
    	else if(scorep1>21)
    		System.out.println("you busted. ");
    	else
    		System.out.println("Dealer busted. ");
    }
    
    }
    But what I think might be causing the problem is actually the deck class...

    Java Code:
    public class Deck {
    	
    	private Card[] deck;
    	private int usedCards;
    	
    	public Deck(){
    			deck = new Card[52];
    			int cardCount=0;
    			for(int suit=0; suit>=3; suit++){
    				for(int value=1;value>=13;value++){
    					deck[cardCount]=new Card(suit, value);
    					cardCount++;
    				}
    			}
    	}
    
    	public void shuffle(){
    		for(int i=51; i>0; i--){
    			int random = (int)(Math.random()*52);
    			Card temp = deck[i];
    			deck[i]=deck[random];
    			deck[random]= temp;
    		}
    		usedCards=0;		
    	}
    	
    	public int getCardsUsed(){
    		return usedCards;
    	}
    	
    	
    	public Card dealCard(){
    		usedCards++;		
    		return deck[usedCards-1];
    	}
    	public String toString(int i){
    		Card c = deck[i];
    		return c.toString();
    	}
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yep, you'd better check what Deck is dealing. I hope your Card class has a toString override method, and use that in your code above. For e.g., something like:

    Java Code:
          Card c = d.dealCard();
          System.out.println(c);
          p1.addCard(c);
          c = d.dealCard();
          System.out.println(c);
          p1.addCard(c);
          c = d.dealCard();
          System.out.println(c);
          p2.addCard(c);
          c = d.dealCard();
          System.out.println(c);
          p2.addCard(c);

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yep, System.out.println's will help you solve this. Focus on your Deck constructor, especially the nested for loops, especially the boolean conditions in the for loop.

    BTW your for loops should avoid using magic numbers if possible. Use array lengths when available and constants when not (though this isn't your major problem).

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    But what I think might be causing the problem is actually the deck class...
    Computer Says No...
    Exception in thread "main" java.lang.NullPointerException
    at Player.evaluate(Player.java:29)
    at Player.hit(Player.java:18)
    //Game.java:23
    while(p2.hit())
    Following Player.hit() takes you to Player.evaluate() where the null pointer is found.

    I think the problem is that you can't you the += operator on this variable before it is initialised:
    Java Code:
    private int runningTotal;

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ozzyman View Post
    Computer Says No...


    //Game.java:23


    Following hit() takes you to evaluate() where the null pointer is found.

    I think the problem is that you can't you the += operator on this variable before it is initialised:
    Java Code:
    private int runningTotal;
    Yeah, it is the Deck class. Please see my post above. I solved it creating my own Card and Dealer classes (the OP neglected to post these), and giving all classes toString methods, and then calling these methods. Deck always held an array of null cards and the constructor is at fault. I'm not saying though that there aren't other problems with his code, just that the NPE is coming from the Deck class code.

  9. #9
    dylandrop is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    Arghghhfdjskahfjdks

    thanks fubarable. found the problem. I always make these stupid mistakes.

  10. #10
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    I've just tried this out and I can confirm this is your problem.

    int otherNumber = 0;
    int someNumber;
    someNumber += otherNumber;

    //error

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dylandrop View Post
    Arghghhfdjskahfjdks

    thanks fubarable. found the problem. I always make these stupid mistakes.
    If you're human like the rest of us, you always will keep doing this -- but don't give up. Glad we helped.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    As an aside, this type of program with playing cards is the classic case for using enums as the suits and values of the Cards model quite well to the enum construct. In fact you have to look no further than the Oracle Java enum tutorials for an example of this: enums tutorial

  13. #13
    dylandrop is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 07:48 PM
  2. Problem with null pointer exception?
    By dunafrothint in forum AWT / Swing
    Replies: 3
    Last Post: 11-11-2009, 04:34 PM
  3. Integration Problem-Null pointer Exception
    By uhdam in forum AWT / Swing
    Replies: 0
    Last Post: 04-07-2009, 12:46 PM
  4. null pointer exception
    By jyothi.priyanka in forum New To Java
    Replies: 12
    Last Post: 03-11-2009, 06:04 PM
  5. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 04:30 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
  •