Results 1 to 6 of 6
Thread: A problem with using an array
- 12-21-2011, 04:34 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
A problem with using an array
Hi! I'm both new here and to java. I was following the java tutorial somewhere and it asked me to practice what I've learned already by writing a class representing a deck of cards. I had a class for single cards written before already. It looks like this:
Java Code:package create; public class Deck extends Card { private Card[] singleCard = new Card[52]; public Deck() { char suit1 = 'X'; String rank1 = "X"; int counter = 0; for(int i = 0 ; i < 14 ; i++){ /* some switches to assign correct suit/card rank go here */ singleCard[counter].modifyCard(suit1, rank1); /* HERE */ counter++; } } } /****************************************/ public void printSingleCard(int cardNo) { System.out.printf(this.singleCard[cardNo].suit+" "+this.singleCard[cardNo].rank + "\n"); } }
But every time I tun it it says there's an error in the line there I put /*HERE*/ comment.
Could anyone help me with this one? What did I do wrong?
Thanks!
- 12-21-2011, 04:38 PM #2
Re: A problem with using an array
What is the exact error?
When do you initialize each index of the array?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-21-2011, 04:41 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Re: A problem with using an array
So I have to initialize every single index of the array, yes? How and where can I do it then?
The Error is
Exception in thread "main" java.lang.NullPointerException
at create.Deck.<init>(Deck.java:40)
at create.CreateObjectDemo.main(CreateObjectDemo.java :14)
- 12-21-2011, 04:44 PM #4
Re: A problem with using an array
Yep, when you initialize an array, each index contains the default value of the type contained by the array. For things like ints, that's zero. For Objects, however, that's null.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-21-2011, 04:55 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Re: A problem with using an array
That solved it! Thanks!
- 12-21-2011, 05:11 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Help with Array problem!
By mr2insane in forum New To JavaReplies: 8Last Post: 06-17-2011, 03:49 AM -
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
Array Problem
By Aiquoc in forum New To JavaReplies: 6Last Post: 03-29-2011, 04:03 AM -
Array problem
By binarzt in forum New To JavaReplies: 5Last Post: 02-14-2010, 09:01 AM -
array problem
By oceansdepth in forum New To JavaReplies: 3Last Post: 04-05-2008, 02:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks