1 Reply Latest reply: Sep 24, 2008 2:53 AM by 843785 RSS

    Error using overloaded constructors (with classes as parameters)

    843785

      Tried to search online and in my texts, but have been unable to find an answer (please post link and deepest apologies if topic has been addressed).



      Hard to explain the exact program setup in words, so I'll put the simplied code. Basically I'm trying to print out the names of weapons I put into an array of Infantry object's "hands". I print out two weapons if they are carrying two weapons (one for each hand), but only one if they are carrying just one, which will always be in the Infantry's left hand. Weapons fall into two categories: Ranged and CloseCombat.

      When I start printing it out, it works for the leader who is carrying two weapons, but dies when it reaches the first troop carrying a rifle (in his left hand and nothing in his right).

      I tested the code and when all Infantry are carrying two weapons like the Leader it works OK.

      Any help is appreciated.



      //---begin code



      import java.util.*;

      public class Test {
      public static void min(String[] args) {

      //create weapons
      RangedWeapon Rifle = new RangedWeapon("Rifle", 4, 10);

      RangedWeapon Pistol = new RangedWeapon("Pistol" 3, 5);

      CloseCombatWeapon Knife = new CloseCombatWeapon("Knife");

      // now create infantry soldiers
      ArrayList<Infantry> squad = new ArrayList<Infantry>();

      // leader of the squad will have a pistol + knife
      int i = 0;

      Infantry leader = new Infantry("Leader", Pistol, Knife);
      squad.add(leader);

      // the other 4 troops in the squad will have a rifle

      for (i = 1; i < 5; i++) {
      Infantry troop = new Infantry("Troop", Rifle);
      squad.add(troop);
      }

      // display the name and weapon for each person in the squad

      for (Infantry infantryToSet : squad) {
      System.out.println('Name :" + infantryToSet.getName()); // call getName() method in NonVehicle class

      // This line works
      System.out.println("Weapon 1: " + infantryToSet.getCurrentLeftArmWeapon().getName());

      // But this line does not. Here is where it seems to die.
      {color:#ff0000} System.out.println("Weapon 2: " + infantryToSet.getCurrentRightArmWeapon().getName());{color}
      } //end loop
      } //end main
      } //end class

      //------Output-----
      Name: Leader
      Weapon 1: Pistol
      Weapon 2: Knife
      Name: Troop
      Weapon 1: _{color:#0000ff}RifleException{color}_ in thread "main" _{color:#0000ff}java.lang.NullPointerException{color}_ {color:#ff0000}at Test.main({color}{color:#0000ff}_Test.java:155_{color}{color:#ff0000}){color}

      //-----------
      public class Infantry extends NonVehicle {

      {color:#0000ff}private Weapon leftArmWeapon;
      private Weapon rightArmWeapon;{color}

      // Code which returns the value of instance variables
      public Weapon {color:#000000}getCurrentLeftArmWeapon() {
      {color} return {color:#0000ff}leftArmWeapon;{color}
      }


      public Weapon getCurrentRightArmWeapon() {
      return {color:#0000ff}rightArmWeapon{color};
      }

      //Constructor using where an Infantry is carrying two weapons
      public Infantry (String aname, {color:#0000ff}Weapon aleftArmWeapon{color}, {color:#0000ff}Weapon arightArmWeapon{color}) {
      super(aname);
      this.{color:#0000ff}leftArmWeapon{color} = {color:#0000ff}aleftArmWeapon{color};
      this.{color:#0000ff}rightArmWeapon{color} = {color:#0000ff}arightArmWeapon{color};
      }

      //Constructor using where an Infantry is carrying just one weapon
      public Infantry (String aname, Weapon aleftArmWeapon) {
      super(aname);
      this.{color:#0000ff}leftArmWeapon{color}{color:#0000ff} = {color}{color:#0000ff}aleftArmWeapon{color}{color:#0000ff};{color}
      }
      } // end class

      //---------------------------------------------

      public abstract class NonVehicle {
      private String name;

      public String getName() {
      return name;
      }

      // constructor
      public NonVehicle (String aname) {
      this.name = aname;
      }
      } //end class

      //-----------------------------------------------

      public class Weapon {
      private String name;
      private int strength;

      public String getName() {
      return name;
      }

      // Constructor if no weapon specified
      public Weapon() {
      this.name = "None";
      this.strength = 0;
      }

      // Constructor for ranged weapon
      public Weapon (String aname, int astrength) {
      this.name = aname;
      this.strength = astrength;
      }

      // Constructor for a close combat weapon
      public Weapon (String aname) {
      this.name = aname;
      }
      } //end class

      //---------------------------

      public class RangedWeapon extends Weapon {
      private int range;

      // constructor
      public RangedWeapon(String aname, int astrength, int arange) {
      super(aname, astrength);
      this.range = arange;
      }
      } //end class

      //---------------------------

      public class CloseCombatWeapon extends Weapon {

      // constructor
      public CloseCombatWeapon(String aname) {
      super(aname);
      }
      } //end class