2 Replies - 560 Views - Last Post: 20 February 2012 - 01:10 PM Rate Topic: -----

#1 leontd  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 06-January 12

DistanceTo - Stackoverflowerror

Posted 20 February 2012 - 12:42 PM

Hello, I am writing a point program and came to a StackoverFlowErorr when I used the distanceTo method. Any help would be very appreicated!

Here is the distanceTo method that I have
public double distanceTo(Point other)
    {
        return distanceTo(other);
    }


And this is the driver that used the method:
System.out.print("expected: ???\ngot:      "); 
			p1 = new Point(2,1);
			p2 = new Point(5,4); 
			System.out.println(p1.distanceTo(p2));


Is This A Good Question/Topic? 0
  • +

Replies To: DistanceTo - Stackoverflowerror

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: DistanceTo - Stackoverflowerror

Posted 20 February 2012 - 12:48 PM

Its because you have an infinity loop of recursion.

In your distanceTomethod you call distanceTo. In that method you then call distanceTo again which then calls it again and again and so on.

Because you are calling so many methods, the stack (which holds information on the caller method and some other important things) is not big enough to hold all of these method calls (there are a lot) - which then causes this error.

I'm not sure of your design but you don't want to be calling that method again inside of itself in this instance.
Was This Post Helpful? 2
  • +
  • -

#3 leontd  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 06-January 12

Re: DistanceTo - Stackoverflowerror

Posted 20 February 2012 - 01:10 PM

Your amazing, thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1