Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StackOverflowError with 'this'

 
amit k prabhat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all ,
when try to use 'this' with toString(), I got StackOverflowError..
There is no compile time error but at run time I'm getting error.
Is this happens because 'this'(current obj ref) also try to call toString() implicitly and end with StackOveflowError..then what about next line of error message.. java.lang.AbstractStringBuilder.append(Unknown Source)
UnKnown source why??
please discuss this situation..


class Base{
public String toString(){
return ""+this;
}

public Base returnBase(){
return this;
}


}
public class CheckThis{
public static void main(String [] args){
Base b=new Base();
System.out.println(b.returnBase());
// or simply call System.out.println(b); or b.toString();
}
}

/* run time error
Exception in thread "main" java.lang.StackOverflowError
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at Base.toString(CheckThis.java:3)
*/
 
Quirino Gervacio
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amit! I will try to re-create your error.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This error mostly occurred when your program end up in an infite recursive loop and stack assigned for each method call gets exhausted (Hope I used correct word!) .

You are calling Base#toString() methods recursively w/o any exit condition !, See where might be the problem hidden (Hint : look, what happens when you concatenate the string (using + ) with string literal )

 
Quirino Gervacio
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It seems that in line 5, appending "" to this causes the operation to call toString(), which in case your own toString(), which calls itself.
""+this and ""+this.toString() are the same.

I hope this helps.
[ August 09, 2008: Message edited by: Quirino Gervacio ]
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! great...The error is indeed in the line ""+this. The toString method is supposed to generate a new string and not depend on the ""+ to generate a string representation for the object..
I would have died in finding the reason for this problem...
 
Clarence J M Tauro
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit,
Quirino is perfect.
Any object when concatenated with String, Object's reference is converted to string using the "toString" method in the form of ObjectName@someAddress. So here in your problem, when you are writing as 'return "" + this', here 'this' is called as this.toString().. and which toString(), its your method that you had overridden.

Just remove the toString method from your class, and then call returnBase(), you see that your reference is being printed, why? toString of the super class is called.

Thats it..
 
amit k prabhat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all java_lovers for their respective views n suggestions...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic