Can't find Null Pointer Exception
Hi everybody. I'm writing a Java program to simulate the Risk board game, and I've come across a Null Pointer Exception, and I can't figure out where it's coming from. I've copied over what I think are the necessary parts from each class so that maybe you could help out:
public class Main
{
public static void main(String[] args)
{
World earth = new World();
earth.addCountry("Eastern United States", "North America");
}
...
public class World
{
private ArrayList<Country> countries;
private Deck deck;
public World(){
countries = new ArrayList<Country>();}
public void addCountry (String newName, String newContinent){
Country temp = new Country(newName, newContinent);
countries.add(temp);
deck.addCard(temp.getAssociatedCard()); // This is where it flags the null pointer exception
}
}
...
public class Deck
{
private ArrayList<Card> deck = new ArrayList<Card>();
public Deck(){}
public void addCard(Card theCard){
deck.add(theCard);
}
}
...
public class Card
{
private String cardType; //either "INFANTRY", "CAVALRY", or "CANNON"
private Country image; // the image on the card.
public static String lastCardType = "CANNON";
//constructors
public Card(Country newCountry){
if (lastCardType.equals("INFANTRY"))
{
cardType = "CAVALRY";
lastCardType = "CAVALRY";
}
else if (lastCardType.equals("CAVALRY"))
{
cardType = "CANNON";
lastCardType = "CANNON";
}
else if (lastCardType.equals ("CANNON"))
{
cardType = "INFANTRY";
lastCardType = "INFANTRY";
}
image = newCountry;
}
}
...
public class Country
{
private String name; // name of country
private String color; // what color player owns this territory
private String continent; // what continent the territory is on
private Integer armies; // how many armies occupy this area.
private Card associatedCard; // the associated card with this territory.
private ArrayList<Country> links = new ArrayList<Country>(); // an array of what territories are connected.
//constructors
public Country(){
name = "NULL";
color = "NULL";
continent = "NULL";
armies = 0;
}
public Country(String newName, String newContinent){
name = newName;
color = null;
continent = newContinent;
armies = 0;
associatedCard = new Card(this);
}
public Card getAssociatedCard(){
return associatedCard;
}
}
So, here's what I'm trying to accomplish:
World is a class that controls what countries are on the board and what's in the deck. Deck is a class that controls what cards are available. Cards can be added to it with the addCard method. The Card class consists of a String containing the name of its representative country and either "INFANTRY", "CAVALRY", and "CANNON", which it alternates through with each new card constructed. The Country class has various bits of information, including a name of the country and its associated Card. When a new country is constructed, and new Card is constructed as well.
So, the program structure I'm looking for is:
earth is a World.
add the Country called "Eastern United States" in the continent "North America".
when we do this, we will also create a card called "Eastern United States" with a String with the value "INFANTRY".
Put that country in the world and put its respective card in the current deck.
But I get the Null Pointer Exception at the place I marked (in World under the addCountry method). Can anybody help me out?

