Objects within classes

Hi, I haven't programmed in Java for four years, but I have a project that is forcing me to get back into it.

Essentially, I am simulating an artifical world. I have a class called 'State' and it needs to have a Vector 'allies.'

I can't figure out how to instantiate State and make this Vector not be null. I've tracked it down and I am sure that when 'allies' is used, it comes back null.

I have tried a number of different things, but I just can't get the Vector to not be null.

I pasted to source code for the class below.

Thanks,

Andy

public class State {

// fields

public int power;

public double aggressiveness;

public double threatTolerance;

public boolean independent;

public Vector allies;

// constructor

public State(int startPower, double startAggressiveness, double startThreatTol) {

power = startPower;

aggressiveness = startAggressiveness;

threatTolerance = startThreatTol;

independent = true; // all states begin independent

Vector allies = new Vector();

Coordinates dummy = new Coordinates(-1, -1);

Ally dummyA = new Ally(dummy, dummy);

allies.addElement(dummyA);

//allies.removeAllElements(); // states start with no allies

}

// methods

public void addAlly(Ally newAlly){

allies.addElement(newAlly);

}

}

[1423 byte] By [bauschawa] at [2007-11-26 16:15:30]
# 1
threatTolerance = startThreatTol;independent = true; // all states begin independentallies = new Vector();// Note the difference
sabre150a at 2007-7-8 22:38:26 > top of Java-index,Java Essentials,New To Java...
# 2

Sounds like you have called new State(), which won't have instaniated your attributes. You need to add the base constructor public class State

{

private List allies;

int power;

public State()

{

allies = new ArrayList();

}

public State(int power)

{

super();

this.power = power;

}

}

Hope thats enuff code to make sense.

Ted.

Message was edited by:

ted_trippin - P.S. Vectors are old school, dont use them!

ted_trippina at 2007-7-8 22:38:26 > top of Java-index,Java Essentials,New To Java...
# 3
Fixed it - thanks
bauschawa at 2007-7-8 22:38:26 > top of Java-index,Java Essentials,New To Java...