# DistanceTo - Stackoverflowerror

Page 1 of 1

## 2 Replies - 1116 Views - Last Post: 20 February 2012 - 01:10 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=267474&amp;s=327cc25b06c10b8a48cef5d00453d4a5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 leontd

Reputation: 1
• 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

• D.I.C Lover

Reputation: 1458
• Posts: 3,285
• 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.

### #3 leontd

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

## Re: DistanceTo - Stackoverflowerror

Posted 20 February 2012 - 01:10 PM