Results 1 to 6 of 6
  1. #1
    Czuks is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default 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!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default 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!

  3. #3
    Czuks is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default 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)

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default 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!

  5. #5
    Czuks is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: A problem with using an array

    That solved it! Thanks!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: A problem with using an array

    Just a comment.
    When you are extending a class you are essentially saying that the <child class> is-a <parent class>.
    In your code above you are saying a Deck is-a Card.

Similar Threads

  1. Help with Array problem!
    By mr2insane in forum New To Java
    Replies: 8
    Last Post: 06-17-2011, 04:49 AM
  2. Replies: 0
    Last Post: 05-12-2011, 08:04 PM
  3. Array Problem
    By Aiquoc in forum New To Java
    Replies: 6
    Last Post: 03-29-2011, 05:03 AM
  4. Array problem
    By binarzt in forum New To Java
    Replies: 5
    Last Post: 02-14-2010, 10:01 AM
  5. array problem
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-05-2008, 03:25 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
  •