Results 1 to 4 of 4
  1. #1
    smande3 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default ive tried a million ways but cant call a simple method on an array list object Help

    Maybe im going about this whole thing in a dumb way but im just trying to construct a deck of cards and can not figure it out my problem always comes to null pointer reference when i call a arraylist.get(index).getsomething() please help me

    class creates card objects:

    Java Code:
    import java.util.ArrayList;
    
    class Card  {
    	int value;
    	String rank;
    	String suit;
    	String cardRankAndSuit;
    	
    	public Card(int v)  {
    		value = v;
    		
    		String [] ranks = {"Ace", "King", "Queen", "Jack", "Ten", "Nine", "Eight", "Seven", "Six", "Five", "Four", "Three", "Two"};		
    
    		for(int i = 0; i<13; i++){	
    			if( (value == i) || (value == i+13) || (value == i+26) || (value == i+39) ) {
    				rank = ranks[i];
    				}
    			}
    
    		if(value <13){
    			suit = "Spades";
    			}
    		if(value >12 && value <26){
    			suit = "Hearts";
    			}
    		if(value >25 && value <39){
    			suit = "Diamonds";
    			}
    		if(value >38){
    			suit = "Clubs";
    			}
    
    		cardRankAndSuit = rank + " of " + suit;
    		}
    
    	public int getValue()  {
    		return value;
    		}
    
    	public String getCardRankAndSuit()  {
    		return cardRankAndSuit;
    		}	
    
    	}

    class creates deck to hold cards ( and is supposed to call from the deck array list a specific hard (problem area in bold):

    Java Code:
    import java.util.ArrayList;
    
    class Deck  {
    
    	boolean hasCards;
    	ArrayList<Card> deck;
    	int position;
    	String cardSpoken;
    	
    	
    
    	public void setUpDeck()  {
    		ArrayList<Card> deck = new ArrayList<Card>();
    		hasCards = true;
    
    		for(int i = 0; i<52; i++){
    			Card card = new Card(i);
    			deck.add(card);
    			}
    		}
    
    [B]	public void pickCard(int p)  {
    		position = p;
    		cardSpoken = deck.get(position).getCardRankAndSuit();
    		}[/B]
    	public String getPickedCard()  {
    		return cardSpoken;
    		}
    
    	}
    simple test class to pick a random card and display it:

    Java Code:
    class DealOutDeck  {
    
    
    
    	public static void main(String  [] args)  {
    		Deck newDeck = new Deck();
    		newDeck.setUpDeck();
    		int randomCardInDeck = (int) (Math.random() * 52);
    		newDeck.pickCard(randomCardInDeck);
    		System.out.println(newDeck.getPickedCard());
    			}
    		}

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: ive tried a million ways but cant call a simple method on an array list object He

    Your Deck class declares two different deck variables. The one you declare in setUpDeck() will hide the instance variable one and, because it is a local variable, it will disappear when the setUpDeck() method finishes.

    -----

    If you still have trouble post the stack trace associated with the NullPointerException.

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: ive tried a million ways but cant call a simple method on an array list object He

    Quote Originally Posted by pbrockway2 View Post
    Your Deck class declares two different deck variables. The one you declare in setUpDeck() will hide the instance variable one and, because it is a local variable, it will disappear when the setUpDeck() method finishes.
    Put simply, code on line 13 should read
    Java Code:
    deck = new ArrayList<Card>();
    note the missing declaration.

    Regards.

  4. #4
    smande3 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: ive tried a million ways but cant call a simple method on an array list object He

    Thank you both. Works fine now

Similar Threads

  1. Replies: 2
    Last Post: 05-03-2012, 08:12 PM
  2. Replies: 3
    Last Post: 03-28-2012, 12:23 AM
  3. read or call an array from other class an method
    By ki_ha1984 in forum New To Java
    Replies: 5
    Last Post: 01-23-2012, 11:42 PM
  4. Replies: 1
    Last Post: 11-29-2011, 02:30 PM
  5. Different ways of creating object
    By krashh205 in forum New To Java
    Replies: 1
    Last Post: 12-13-2010, 08:19 PM

Posting Permissions

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