Question

Why am I getting a stackoverflow error ?

My class -

public class Tester {

int id;

 Tester(int id){
  this.id = id;
 }

 public String toString(){

  String rep = "Hex: " + this + ", Id: " + this.id;
  return rep;
 }

}

The main method -

class Driver{

    public static void main(String [] args){

        Tester t = new Tester(123);
        System.out.println(t);

    }

}

Error -

Exception in thread "main" java.lang.StackOverflowError
    at java.lang.String.length(Unknown Source)
    at java.lang.AbstractStringBuilder.append(Unknown Source)
    at java.lang.StringBuilder.append(Unknown Source)
    at java.lang.StringBuilder.<init>(Unknown Source)
    at com.examscam.model.Tester.toString(Tester.java:13)
    at java.lang.String.valueOf(Unknown Source)
    at java.lang.StringBuilder.append(Unknown Source)

---------REPEAT !!!
Was it helpful?

Solution 2

You wrote:

String rep = "Hex: " + this + ", Id: " + this.id;

In java simply writing this means that you are indirectly invoking this.toString().

I believe you are trying to override the toString() method of Object and inside your version of toString() you want to print the id you have passed along with the hashcode of the object.

So to get the output replace

String rep = "Hex: " + this + ", Id: " + this.id;

with

String rep = "Hex: "+ this.getClass().getName().hashCode() +", Id: " + id;

and you will get the output as:

Hex: 1800024669, Id: 123

OTHER TIPS

Because

"Hex: " + this

is equivalent to

"Hex: " + this.toString()

and you're doing that from the toString(), so toString() calls itself, which calls itself, which calls itself...

you are using this keyword.

String rep = "Hex: " + this + ", Id: " + this.id;

This represent the current object. Your current object is being called again and again recursivley so you are getting

java.lang.StackOverflowError

You are appending "this". This calls the toString() method, which again calls toString(), ... It's an infinite recursion loop, which does not have an end.

Because you are referencing this in toString()

That means that this.toString() is being called, therefor infinite recursion is occurring

Your toString method is the culprit,

String rep = "Hex: " + super.toString() /* Not this */
   + ", Id: " + this.id;

thi line

String rep = "Hex: " + this + ", Id: " + this.id;

would become

String rep = "Hex: " + this.toString() + ", Id: " + this.id;

at run-time and will again call your class's toString..wi again..

In the line String rep = "Hex: " + this + ", Id: " + this.id;

  this

is equivalent to

 this.toString()

and calling it from the toString(), will again call toString and again...

I think you are overriding toString method and in your overrided method body your calling your method again! you are calling toString by writing this+"" in toString method

String rep = "Hex: " + this + ", Id: " + this.id;

equal to String rep = "Hex: " + this.toString() + ", Id: " + this.id; internally

this will leads to recursive method call that will result in

 java.lang.StackOverflowError

Because using first this in your toString method goes on calling the toString method recursively resulting in a stackoverflow.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top