-2

When I run javac Hero.java Then java Hero The program spits out tons of lines that look like this:

Hero@322ba3e4
Hero@4f14e777
Hero@65685e30
Hero@26ffd553
Hero@660e5025
Hero@35afe17b

What is this? Hero@322ba3e4

Then it shows,

Exception in thread "main" java.lang.StackOverflowError
    at sun.nio.cs.UTF_8.updatePositions(UTF_8.java:77)
    at sun.nio.cs.UTF_8$Encoder.encodeArrayLoop(UTF_8.java:564)
    at sun.nio.cs.UTF_8$Encoder.encodeLoop(UTF_8.java:619)
    at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:561)
    at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:271)
    at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
    at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
    at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129)
    at java.io.PrintStream.write(PrintStream.java:526)
    at java.io.PrintStream.print(PrintStream.java:669)
    at java.io.PrintStream.println(PrintStream.java:823)
    at Hero.DrowRanger(Hero.java:78)
    at Hero.DrowRanger(Hero.java:79)

This line repeats 100s of times:

at Hero.DrowRanger(Hero.java:79)

This is the method at that line,

            private static Object DrowRanger() {
            Hero DrowRanger = new Hero(
                  0,
                  "Agility",
                  "Ranged",
                  "Frost Arrows",
                  "Gust",
                  "Precision Aura",
                  "Marksmanship",
                  17,
                  1.9,
                  26,
                  1.9,
                  15,
                  1.4,
                  473,
                  195,
                  44,
                  55,
                  625,
                  0.64,
                  300);

       System.out.println(DrowRanger);
 return DrowRanger();                // Line 79 Is Here      
}

This is the full class

public class Hero implements NPC {

        private int level;
        private String primaryAttribute;
        private String attackType;
        private String ability1;
        private String ability2;
        private String ability3;
        private String ability4;
        private double strength;
        private double strengthMultiplier;
        private double agility;
        private double agilityMultiplier;
        private double intelligence;
        private double intelligenceMultiplier;
        private int health;
        private int mana;
        private int damageMin;
        private int damageMax;
        private int range;
        private double armor;
        private int movement;

        //default constructor
        public Hero(
                int level,
                String primaryAttribute,
                String attackType,
                String ability1,
                String ability2,
                String ability3,
                String ability4,
                double strength,
                double strengthMultiplier,
                double agility,
                double agilityMultiplier,
                double intelligence,
                double intelligenceMultiplier,
                int health,
                int mana,
                int damageMin,
                int damageMax,
                int range,
                double armor,
                int movement
                    ) {

        } // End Constructor

        public static void main (String[] args) {
            DrowRanger();

            }

        private static Object DrowRanger() {
        Hero DrowRanger = new Hero(
              0,
              "Agility",
              "Ranged",
              "Frost Arrows",
              "Gust",
              "Precision Aura",
              "Marksmanship",
              17,
              1.9,
              26,
              1.9,
              15,
              1.4,
              473,
              195,
              44,
              55,
              625,
              0.64,
              300);

        System.out.println(DrowRanger);
        return DrowRanger();
}


        // getters and setters - required to implement ALL from interface
        public int getLevel() {
            return this.level;
        }

        public String getPrimaryAttribute() {
            return this.primaryAttribute;
        }

        public String getAttackType() {
            return this.attackType;
        }

        public String getAbility1() {
            return this.ability1;
        }

        public String getAbility2() {
            return this.ability2;
        }

        public String getAbility3() {
            return this.ability3;
        }

        public String getAbility4() {
            return this.ability4;
        }

        public double getStrength() {
            return this.strength;
        }

        public double getStrengthMultiplier() {
            return this.strengthMultiplier;
        }

        public double getAgility() {
            return this.agility;
        }

        public double getAgilityMultiplier() {
            return this.agilityMultiplier;
        }

        public double getIntelligence() {
            return this.intelligence;
        }

        public double getIntelligenceMultiplier() {
            return this.intelligenceMultiplier;
        }

        public int getHealth() {
            return this.health;
        }

        public int getMana() {
            return this.mana;
        }

        public int getDamageMin() {
            return this.damageMin;
        }

        public int getDamageMax() {
            return this.damageMax;
        }

        public int getRange() {
            return this.range;
        }

        public double getArmor() {
            return this.armor;
        }

        public int getMovement() {
            return this.movement;
        }

    // This is where the setters are.

        public void setLevel(int level) {
            this.level = level;
        }

        public void setPrimaryAttribute(String primaryAttribute) {
            this.primaryAttribute = primaryAttribute;
        }

        public void setAttackType(String attackType) {
            this.attackType = attackType;
        }

        public void setAbility1(String ability1) {
            this.ability1 = ability1;
        }

        public void setAbility2(String ability2) {
            this.ability2 = ability2;
        }

        public void setAbility3String(String ability3) {
            this.ability3 = ability3;
        }

        public void setAbility4(String ability4) {
            this.ability4 = ability4;
        }

        public void setStrength(double strength) {
            this.strength = strength;
        }

        public void setStrengthMultiplier(double strengthMultiplier) {
            this.strengthMultiplier = strengthMultiplier;
        }

        public void setAgility(double agility) {
            this.agility = agility;
        }

        public void setAgilityMultiplier(double agilityMultiplier) {
            this.agilityMultiplier = agilityMultiplier;
        }

        public void setIntelligence(double intelligence) {
            this.intelligence = intelligence;
        }

        public void setIntelligenceMultiplier(double intelligenceMultiplier) {
            this.intelligenceMultiplier = intelligenceMultiplier;
        }

        public void setHealth(int health) {
            this.health = health;
        }

        public void setMana(int mana) {
            this.mana = mana;
        }

        public void setDamageMin(int damageMin) {
            this.damageMin = damageMin;
        }

        public void setDamageMax(int damageMax) {
            this.damageMax = damageMax;
        }

        public void setRange(int range) {
            this.range = range;
        }

        public void setArmor(double armor) {
            this.armor = armor;
        }

        public void setMovement(int movement) {
            this.movement = movement;
        }

    } // End Character Class
`
ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
wuno
  • 9,547
  • 19
  • 96
  • 180

2 Answers2

4

Your DrowRanger method is calling itself infinitely. This is almost always the cause of a StackOverflowException.

This line:

return DrowRanger(); 

Calls the DrowRanger method, but since you're inside the DrowRanger method it just keeps calling itself infinitely. I think you meant to return the local object:

return DrowRanger;

In general, it is a bad idea to give methods and objects the same name. Also, it is java convention always start method and variable/field names with a lowercase letter.

The output you're seeing is how java prints objects that have not overridden the toString() method. See this post for an explanation of how the toString() method works.

Community
  • 1
  • 1
ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
  • Ahh yes thank you. Also I need to create a toString method and pur DrowRanger there? – wuno Jul 27 '14 at 21:53
  • Thank you so much for answering me and not down voting. You were right about both problems. Can you illustrate for me how to properly use the toString? – wuno Jul 27 '14 at 21:58
0

To the first: println prints the output of the toString method of the object. As it is just the normal method inherited by the Object class it just returns the name of the class and the hash of the object. To your error: it looks like there is an infinite loop somewhere (which has been found).

schnaidar
  • 190
  • 4
  • 11
  • Would you mind showing me how I can print out the Hero's stats when I run the program? – wuno Jul 27 '14 at 22:20
  • There is a good advice in the Android docs: http://developer.android.com/reference/java/lang/Object.html – schnaidar Jul 27 '14 at 22:33
  • I am confused. I thought at this point since I created a new object DrowRanger in the Hero class I can just return the method getHealth etc? – wuno Jul 27 '14 at 22:39
  • `toString()` should return a textual representation of the object. That is mostly a combination of the properties. You can return the returns of getHealth() etc. if you like to do. – schnaidar Jul 27 '14 at 22:45
  • Yes But How do I do that for DrowRanger? like DrowRanger.getHealth(); – wuno Jul 27 '14 at 22:46
  • No, you should rather use the variables. Something like `return "Health: " + health + "\nMana: " + mana;` – schnaidar Jul 27 '14 at 22:50
  • But how would I specify which Hero's health I am returning? – wuno Jul 27 '14 at 22:51
  • Either you use the hash function or you add a new variable with the name – schnaidar Jul 27 '14 at 22:54