6

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 !!!
Erran Morad
  • 4,563
  • 10
  • 43
  • 72
  • What is ur intention to do in toString method – Vineet Singla Apr 28 '14 at 07:00
  • @VineetSingla - to show the value of `this`. But obviously, I can't do that without causing a SO error. – Erran Morad Apr 28 '14 at 07:03
  • Printing this will just print the classname@hashcode values, Tester class has only one attribute id, so just printing the id should work. – Vineet Singla Apr 28 '14 at 07:06
  • possible duplicate of [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – Raedwald Apr 28 '14 at 07:24
  • Possible duplicate of http://stackoverflow.com/questions/214741/what-is-a-stack-overflow-error – Raedwald Apr 28 '14 at 07:24

11 Answers11

16

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...

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
6

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
Parul S
  • 300
  • 1
  • 3
  • 10
4

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

Rahul
  • 3,479
  • 3
  • 16
  • 28
4

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.

MrP
  • 1,291
  • 1
  • 9
  • 18
4

Because you are referencing this in toString()

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

geoand
  • 60,071
  • 24
  • 172
  • 190
2

Your toString method is the culprit,

String rep = "Hex: " + super.toString() /* Not this */
   + ", Id: " + this.id;
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

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..

niiraj874u
  • 2,180
  • 1
  • 12
  • 19
1

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...

Vineet Singla
  • 1,609
  • 2
  • 20
  • 34
0

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

Blue Ocean
  • 282
  • 1
  • 10
0
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
Naveen
  • 535
  • 3
  • 14
0

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

Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78