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?

[3891 byte] By [austinabramsa] at [2007-11-26 20:21:31]
# 1

The error message will tell you exactly which line it's on. Which line is it?

When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

jverda at 2007-7-10 0:46:21 > top of Java-index,Java Essentials,Java Programming...
# 2
read the [url= http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] and repost.
corlettka at 2007-7-10 0:46:21 > top of Java-index,Java Essentials,Java Programming...
# 3

Oh, I didn't see that you already told us which line.

deck.addCard(temp.getAssociatedCard()); // This is where it flags the null pointer exception

Either deck or temp is null.

Use a debugger or add a bunch of print statements to your program so that you can what's happening at each step and where it diverges from what you think is happening.

jverda at 2007-7-10 0:46:21 > top of Java-index,Java Essentials,Java Programming...
# 4

Oh, sorry. As you can see by my post count, I'm very very new.

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 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());

}

}

...

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;

private Country image;

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 Card(Card copy){

cardType = copy.getCardType();

image = copy.getImage();

}

public Card(){

cardType = null;

image = null;

}

public Card(Country theCountry, String theCardType){

cardType = theCardType;

image = theCountry;

}

}

...

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

}

}

austinabramsa at 2007-7-10 0:46:21 > top of Java-index,Java Essentials,Java Programming...
# 5
Thanks jverd! I didn't construct any new deck, and that fixes the problem.Looking back on it, it seems like such a dumb mistake.
austinabramsa at 2007-7-10 0:46:21 > top of Java-index,Java Essentials,Java Programming...
# 6

Ok, start from the top...

"Design twice. Code once."

... it's just that your written description of the problem lacks "simplicity", which is a clue that you've got a design problem, as apposed to implementation problem.

I suggest yo draw a class diagram which clearly states the relationships between your classes... it doesn't have to be pretty.... the back of a *** packet and a green crayon will do.

Once you've clarified your design in your own mind your good to go with the implementation... if you start coding before your design is "clarified" then I humbly predict that you'll find programming to be "no fun".

keith.

corlettka at 2007-7-10 0:46:21 > top of Java-index,Java Essentials,Java Programming...
# 7
the messege give you the line who's have an error ....
java.oldnicka at 2007-7-10 0:46:21 > top of Java-index,Java Essentials,Java Programming...