Results 1 to 4 of 4
- 06-22-2012, 07:01 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 5
- Rep Power
- 0
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):
simple test class to pick a random card and display it: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; } }
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()); } }
- 06-22-2012, 07:26 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,544
- Rep Power
- 11
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.
- 06-22-2012, 05:51 PM #3
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
- 06-22-2012, 06:54 PM #4
Member
- Join Date
- Jun 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
call method in another class with array ? (is possible?)
By Fotis in forum New To JavaReplies: 2Last Post: 05-03-2012, 07:12 PM -
ArrayList problem, cant call method from object in array.
By Chip in forum New To JavaReplies: 3Last Post: 03-27-2012, 11:23 PM -
read or call an array from other class an method
By ki_ha1984 in forum New To JavaReplies: 5Last Post: 01-23-2012, 10:42 PM -
Unable to call setRedirectStrategy() method through object DefaultHttpClient Class
By aashish.kkalra@gmail.com in forum Advanced JavaReplies: 1Last Post: 11-29-2011, 01:30 PM -
Different ways of creating object
By krashh205 in forum New To JavaReplies: 1Last Post: 12-13-2010, 07:19 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks