Guy
Guy

Reputation: 1067

How to check if an object has been created yet?

The Problem

I have an Army class that is run twice (by a separate driver class) to create two armies. In that Army class there are the following statements to create new objects from the other class files I have:

  public final Archer archer = new Archer(this);
  public final Catapult catapult = new Catapult(this);
  public final NCatapult ncatapult = new NCatapult(this); //Ninja catapult
  public final Ninja ninja = new Ninja(this);
  public final Horse horse = new Horse(this);
  public final Samurai samurai = new Samurai(this);

The constructors for each of those objects require an "Army" object in their constructor method arguments, hence the this keyword used in the instantiations.

So. Within a method in the Horse class, I create a new "Warrior" object, because a Warrior wasn't instantiated initially in the Army class.

private boolean warriorCreated = false;
  public void dismount(){
    if (!warriorCreated){
      Warrior warrior = new Warrior(myArmy);
      warriorCreated = true;
      myArmy.getOurArmyWindow().getTabbedPane().addTab("(Ethan)", new ImageIcon("images/warriorIcon.jpg"), warrior.genGetPanel()); //This is just adding the Warrior's tab to my GUI. It's not relevant to the problem.
    }
    ...//Some stuff to handle when the method is called and warriorCreated is true
    }//end dismount()

You will notice how it uses the variable "myArmy" in the constructor for the Warrior. That is simply the Army object that was passed when the Horse was instantiated.

public Horse(Army armyInstance){ //A constructor that takes in whatever Army object called it
    myArmy = armyInstance;
  }

Within all of the different instantiated objects, I can access the other opposing Army with the use of a myArmy.getEnemyArmyInstance() method. I don't want to explain in detail how it works and take up even more space than I already am, so just trust me that it works.

This means that usually I can reference an object instantiated by the opposing Army by calling something like Ninja enemyNinja = myArmy.getEnemyArmyInstance().ninja; (I can replace "Ninja" with whatever other object I want to reference).

However, because the Warrior object is instantiated by a Horse class method and not right from the get-go in each Army object, I first want to check if it has been instantiated yet before trying to reference it from a separate object.

This is where I run into trouble. Any time I try to check if a Warrior object has been instantiated yet, it gives me an error at compile-time.

For example, consider this check:

public void checkForWarrior(){
if (myArmy.getEnemyArmyInstance().warrior != null){
  System.out.println("A warrior exists!");
}
else
  System.out.println("There is no warrior!");
}

It throws the error

error: cannot find symbol   
if (myArmy.getEnemyArmyInstance().warrior != null){
                                 ^
symbol:   variable warrior
location: class Army

What I Have Tried

I thought maybe the Warrior object created wasn't a member (that is the correct term, right?) of Army, so I tried adding ".horse" to the line only to get a similar result.

   if (myArmy.getEnemyArmyInstance().horse.warrior != null){
                                          ^
   symbol:   variable warrior
   location: variable horse of type Horse

I even tried adding a boolean that would get turned on in the Horse class when a Warrior is instantiated, and then checking for that first. But no dice.

private boolean mounted = true; //This is in the Horse class and is set to
false when a Warrior is instantiated.

In the other classes:

if (myArmy.getEnemyArmyInstance().horse.getMounted()){
Warrior enemy = myArmy.getEnemyArmyInstance().warrior;//Still gives me an
//error that it cannot find the variable warrior. (I tried adding .horse to
//this, too).

The Question

From objects that aren't the Horse that instantiated it, how do I check to see if a Warrior object has been instantiated yet so that I can reference it from those other objects?

Upvotes: 3

Views: 9929

Answers (1)

Wagner DosAnjos
Wagner DosAnjos

Reputation: 6374

If I understood your problem correctly, you need to add the following to your Army class:

public Warrior warrior;

Then in your Horse class:

  public void dismount(){
    if (myArmy.warrior == null){
      myArmy.warrior = new Warrior(myArmy);
      myArmy.getOurArmyWindow().getTabbedPane().addTab("(Ethan)", new ImageIcon("images/warriorIcon.jpg"), warrior.genGetPanel()); //This is just adding the Warrior's tab to my GUI. It's not relevant to the problem.
    }
    ...//Some stuff to handle when the method is called and warriorCreated is true
    }//end dismount()

I hope this helps.

Upvotes: 3

Related Questions