Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: NullPointerException in a small project

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default NullPointerException in a small project

    While studying a tutorial i was asked to write a simple project that displays a deck and it's cards.Then i would create one and display it's cards.I was also said to have two classes,one for a single card,and one of the deck.So i decided that a good approach is to have an array of singleCards as a field of the deck.However something is not clear to me about the initialization of the array.I think that the no-arguments constructor is called.However something is wrong,so maybe you could help
    The project:
    Cards.java
    package cards;
    public class Cards {
        public static void main(String[] args) {
            Deck pokemonCards = new Deck(10);
            pokemonCards.print();
        }
    }

    SingleCard.java
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package cards;
     
    /**
     *
     * @author User
     */
    public class SingleCard {
     
        private int rank;
        private String suit;
        private String name;
     
        SingleCard(int r, String s, String n) {
            rank = r;
            suit = s;
            name = n;
        }
     
        SingleCard() {
            rank = 0;
            suit = "No colour";
            name = "No name";
        }
     
        public String getName() {
            return name;
        }
     
        public int getRank() {
            return rank;
        }
     
        public String getSuit() {
            return suit;
        }
     
        public void printSuit() {
            System.out.println("Card " + name + "has " + suit + "suit");
        }
     
        public void printRank() {
            System.out.println("Card " + name + "has " + rank + "rank");
        }
     
        public void printName() {
            System.out.println("Card's name is " + name);
        }
    }

    Deck.java
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package cards;
     
    /**
     *
     * @author User
     */
    public class Deck {
     
        private SingleCard[] deck;
        private int number;
     
        Deck(int numberOfCards) {
            deck = new SingleCard[numberOfCards];
            number=numberOfCards;
    //        for(int i=0 ; i<numberOfCards ; i++)
    //        {
    //            
    //        }
        }
     
        public SingleCard getCard(int numberOfCard)
        {
            return deck[numberOfCard];
        }
     
        void print()
        {
            for(int i=0 ; i<number ; i++)
            {
                deck[i].printName();
            }
        }
    }

    and the error
    Exception in thread "main" java.lang.NullPointerException
    	at cards.Deck.print(Deck.java:)
    	at cards.Cards.main(Cards.java:)
    Java Result: 1


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: NullPointerException in a small project

    You are getting that because you never added any SingleCards to your deck. You created an array to hold SingleCard objects, but never actually added any SingleCards to the array, so when you say: deck[i].printName();, you are trying to call the printName() method on a null. The array is NOT automatically populated with SingleCard objects, you have to do that yourself.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: NullPointerException in a small project

    I understand what you said.So i am trying this
    public class Deck {
        private static String[] colours ={ "WHITE","BLACK","RED","GREEN","BLUE","YELLOW"
                ,"ORANGE","MAGENTA","GREY","SILVER","GOLD"};
        private static String[] pokemons = {"ZUBAT","CHARIZARD","MAGMAR","LAPRAS","ONIX",
            "MAGIKARP","GEODUDE","ZAPDOS","ARTICUNO","MEW","MWETO"};
     
        private SingleCard[] deck ;
        private int number;
     
        Deck(int numberOfCards) {
            deck = new SingleCard[numberOfCards];
            number=numberOfCards;
            for(int i=0 ; i<numberOfCards ; i++)
            {
                deck[i] = SingleCard(i,colours[i],pokemons[i]);
            }
        }
    ...
    but the compiler can not see the constructor of SingeCard here
    deck[i] = SingleCard(i,colours[i],pokemons[i]);
    .Is this happening because if you have to call a constructor from another constructor this should be done in first line of the constructor?But at first i should allocate memory,i can not call the SingeCard constructor at the 1st line of Deck constructor.Is my approach for initialization wrong?If so could you suggest me one?

    EDIT : it has no relationship in this case with the first line i stated above..
    Last edited by Samaras; August 9th, 2012 at 10:29 AM.

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: NullPointerException in a small project

    solution found-> new keyword missing!

Similar Threads

  1. Small help needed
    By javabeg in forum Java Theory & Questions
    Replies: 4
    Last Post: March 15th, 2011, 09:50 AM
  2. Replies: 2
    Last Post: August 1st, 2010, 06:29 AM
  3. Small Project
    By 3XiLED in forum Paid Java Projects
    Replies: 7
    Last Post: March 1st, 2010, 08:35 AM
  4. small problem ... I need help
    By Ashar in forum Loops & Control Statements
    Replies: 4
    Last Post: December 4th, 2009, 11:26 AM
  5. Small Project Ideas
    By Freaky Chris in forum Project Collaboration
    Replies: 20
    Last Post: August 12th, 2009, 12:49 PM