# Java Stack Overflow Error

I'm trying to program a GCD algorithm and everything seems to be correct except the StackOverflowError. Here is the code:

`public class Gcd { public static BigInteger gcdNaive(BigInteger a, BigInteger b) { int res = a.compareTo(b); if ( res == 0 ) { BigInteger g = a; return g; } else if ( res == 1 ) { BigInteger h = a.subtract(b); a = h; return gcdNaive(a, b); } else if ( res == -1 ) { BigInteger g = b.subtract(a); b = g; return gcdNaive(a, b); } return BigInteger.ZERO; } public static BigInteger gcdEuclid(BigInteger a, BigInteger b) { if( b == BigInteger.ZERO ) { BigInteger g = a; return g; } else if ( b != BigInteger.ZERO ) { BigInteger g = b; BigInteger h = a.mod(b); a = g; b = h; return gcdEuclid(a, b); } return BigInteger.ZERO; } } `

Exception:

`Exception in thread "main" java.lang.StackOverflowError at Gcd.gcdNaive(Gcd.java:7) at Gcd.gcdNaive(Gcd.java:19) `

The solution for gcdNaive is:

```public static BigInteger gcdNaive(BigInteger a, BigInteger b) { int res = a.compareTo(b);```

``` if(res == 0) { return b; } ```

```if(res == 1) { a = a.subtract(b); return gcdNaive(a, b); } else { b = b.subtract(a); return gcdNaive(a, b); } } ```

The complete solution for gcdEuclid is

`public static BigInteger gcdEuclid(BigInteger a, BigInteger b) {`

``` if(b == BigInteger.ZERO) { return a; } ```

```return gcdEuclid(b, a.mod(b)); } ```

Category:java Views:2 Time:2018-04-15

## Related post

• Java stack overflow error - how to increase the stack size in Eclipse? 2010-01-24

I am running a program that I've written in Java in Eclipse. The program has a very deep level of recursion for very large inputs. For smaller inputs the program runs fine however when large inputs are given, I get the following error: Exception in t

• What is a stack overflow error? 2008-10-18

I'm a complete beginner writing Breakout (the game) in Java. All was going well until I started to get a stack overflow error in the late game. Unfortunately I know that without actually putting the code up online I won't really be able to get help w

• Will this ever result in a stack overflow error? 2010-04-14

Will incrementing the instance variables of an object ever lead to a stack overflow error? For example: This method (java) will cause a stack overflow error: class StackOverflow { public static void StackOverflow (int x) { System.out.println (x) ; St

• Stack Overflow error with ArrayLists 2011-04-14

I'm trying to write some code to look through an image file for groups of pixels that are the same color. The way I do this is I iterate through the image (in the form of a 1d integer array with the color's hash code) by pixel to find a pixel of the

• Stack Overflow error android? 2011-08-26

My code was running fine but suddenly when i ran the code again, it gave me stack overflow error. Following is my code:HelloDatePickerActivity has already been defined. HelloDatePickerActivity daten=new HelloDatePickerActivity(); static final int DAT

• Unable to trace the source of a stack overflow error 2011-10-19

I have a Java application that is working as a service. After several days running I get a stack overflow error. The problem is that the stack frame in my source where the error is being originated is not in the error reported by printStackTrace func

• Stack Overflow error occurs when using recursive fibonacci function 2012-02-14

Here's my code and it runs well with values of 400 to 4000 but once it's about 4mil, I get stack overflow errors. Thanks in advance! public class Fib { static int c=1,b=2; static long sum1=0,sum2=0; static long fib(long a){ if(a==1){ return 1; } if(a

• Stack overflow error in android - help needed to execute 2012-03-11

i get a stack overflow error while trying to draw a qr code in canvas, saying unable to create .dex file and prompting to close eclipse, even i changed memory in eclipse.ini but nothing worked here is the QRcodeActivity.java file package karthick.vam

• stack overflow error with regex for parsing 2013-09-11

I know that there has already been some postings about the stack overflow error with regex and long strings, but they didn't help me and never concern my type of parsing problem. I just try to find the string in parentheses from a mathematical functi

• Stack overflow error when using JQuery/ASP.NET for simple ajax chat 2009-04-07

I am trying to create a simple ajax chat using JQuery and ASP.NET. My code works like this: When the page loads it refreshes the 'chatbox' div with a request to the messages.aspx page, which handles getting new messages from the database and kicks of

• stack overflow error 2009-11-12

i just got my first ever stack overflow when I ran this script: var hlat = 0.00; var hlong = 0.00; var mapdiv = document.getElementById('map'); var map_url = base_url + 'ajax/getPropMap'; var id_url = base_url + 'hotels/gethotel_id'; var id=0; var ma

• QuickReport.ExportToFilter throws "stack overflow" error when used in TWebModule 2009-11-16

I have a web application using the TWebModule component. It runs as a module on Apache. The code below throws a "Stack Overflow" error on the ExportToFilter. The same exact code works fine from a Winforms Application and even a service for that matte

• Stack Overflow Error With This Recursive Program? - C++ 2010-02-21

I'm a programming student in my first C++ class, and recently we were given an assignment to implement a recursive program that finds the first occurrence of a given substring in a given string. For example: int StringIndex("Mississippi", "sip"); //

• Stack overflow error in C# set/get 2010-03-07

I was working on a public comments part of an application on Friday when I got a stack overflow error, which confused me so I thought I'd ask for help. And searching the web using the expression 'stack overflow' is a bit self-defeating! I wanted to d

• stack overflow error when i open Delphi XE IDE 2010-10-31

When i open Delphi XE get this message and i must close IDE. message : Danger: stack overflow - save your work and restart Delphi XE I uninstall Delphi and cpp XE and reinstall them, but already get this message. Why? --------------Solutions---------

• flash as3 - stack overflow error in case / switch statement 2011-02-24

Wow - I'm kind of excited - I get a stack overflow error. I'm not sure why though... This is my code: switch (direction) { case "left" : if (project_array[cp].projectThumb.thumbActive == false){ if (cp>0){ cp--; } checkActive("left") } else { unlo

• GWT embedded ValueProxy stack overflow error 2011-04-20

If I have a PersonProxy {String getName(); PersonProxy getParent();} and I try to load it from server through a RequestFactory provided Request object, it crashes with stack overflow error. It tries to call hashCode() indefinitely. Any ideas why is t

• How to fix stack overflow error in visual C/C++? 2011-07-28

I am writing a file in C/C++ which generates 100000 lines of records (name, int[5] grade, double[5].value). The code should generate 100000 random characters for name and integer for value. I am getting stackoverflow error. Can anyone pls help? -----

• Stack overflow error when adding a member to a class in huge solution 2011-08-17

Please look at this code: class A { int a; }; Then add one more member to class A: class A { int a; int b; }; In my huge solution when I add one more member to a class (like member b in class A) I get stack overflow error. I assume that this is someh

• stack-Overflow error while removing nodes of a tree 2012-01-10

I have a tree structure in this format: Index1 | --Key1 --Value1 Index2 | --Key2 --Value2 Key and Value objects are children of Index objects and there are no index objects in the tree. I'm maintaining array lists for Index objects (indexList), Key o

• AS3 addChild(). Stack Overflow error 2012-01-10

I'm having problems with addChild() from a class. I have a Ball class: package { import flash.display.Sprite; import flash.display.MovieClip; import flash.events.*; import Achievement; public class Ball extends Sprite { //the image I want to add var

• Stack overflow error when importing WSDL into Delphi 7 2012-02-09

I am trying to import a WSDL into Delphi 7 using the WSDLImporter (as well as the WSDLImp command line). When using the WSDLImporter, the memory usage of Delphi just starts increasing until it crashes (no error) or I get a Stack overflow error. When

• Does a stack overflow error trigger a core dump and is it useful if it does? 2012-03-06

I'm having a heck of a time finding an answer to this because of "Stack Overflow"'s site name. Everything online references stackoverflow.com and whatever words I search on instead! wah! My question is: Does a stack overflow error (say from infinite

• Why is my search in BBEdit causing a "stack overflow" error? 2012-03-31

I'm stumped about a "stack overflow" error--"out of stack space (application error code: 12246)--that I'm getting in BBEdit when I do a "replace all", searching for (@article(((?!eprint|@article|@book).)*\r)*)pmid = {(.+)}((((?!eprint|@article|@book)

• Internet Explorer 8 Security Update kb978207 causing stack overflow error, with some HTTPS websites. 2012-03-22

Hello, I'm new to this forum, but have looked through various threads, for the same problem, that I'm having, with two of my PC's. I haven't found this specific problem listed; have seen PLENTY of problems posted, with this update, however. When acce