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
Code :
package cards;
public class Cards {
public static void main(String[] args) {
Deck pokemonCards = new Deck(10);
pokemonCards.print();
}
}
SingleCard.java
Code :
/*
* 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
Code :
/*
* 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
Code :
Exception in thread "main" java.lang.NullPointerException
at cards.Deck.print(Deck.java:)
at cards.Cards.main(Cards.java:)
Java Result: 1
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.
Re: NullPointerException in a small project
I understand what you said.So i am trying this
Code :
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
Code :
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..
Re: NullPointerException in a small project
solution found-> new keyword missing!