• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception -- java.lang.StackOverflowError

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is my first post to any forum, hope I've posted it in the right place ...............
The following is a program to solve a sudoku, the program has some bugs that I am not able to figure out. I've also give the (error) at the end.................
(It definitely is the most inefficient implementation of Sudoku solver)


_____________________________________________________________________

OUTPUT
____________________________________________________________________

run:
3 6 0 0 0 0 0 0 0
0 0 4 2 3 0 8 0 0
0 0 0 0 0 4 2 0 0
0 7 0 4 6 0 0 0 3
8 2 0 0 0 0 0 1 4
5 0 0 0 1 3 0 2 0
0 0 1 9 0 0 0 0 0
0 0 7 0 4 8 3 0 0
0 0 0 0 0 0 0 4 5
Exception in thread "main" java.lang.StackOverflowError
at sudokumain.Sudoku.SudokuSolver(Sudoku.java:78)
at sudokumain.Sudoku.SudokuSolver(Sudoku.java:129)
at sudokumain.Sudoku.SudokuSolver(Sudoku.java:111)
at sudokumain.Sudoku.SudokuSolver(Sudoku.java:108)
etcetera.
 
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mustafa, welcome to CodeRanch.

For future reference when you have a problem, please ShowSomeEffort and TellTheDetails. It's hard for people to help you if you don't post the code that is troubling you. You may also want to keep your post short, and easy to read; so don't post 500 lines of code, or your complete stack trace.

In this case, I think we can give you a little bit of help with your problem. Stack overflows usually occur when you have an infinite recursive loop. Somewhere in your code, you're calling a method that (in)directly keeps calling itself over and over again. You need to add a condition to it that will make your program exit the method at some point.
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, your code tags were positioned wrongly, so I changed them. I also cut off the stack trace to show the lines where it was bubbling up.
 
Mustafa Dasorwala
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan,
Thanks very much for making the changes. I do have a termination condition on line 145.I think there might be a logical flaw in my code which I am not able to figure out.
Let me try to explain what I am trying to do.
+ Currently the objective is to solve the Sudoku given by "String input = "360000000004230800000004200070460003820000014500013020001900000007048300000000045"; " (line 64)
+ (ignore line 99 to 102)
+Line 67 to 79 -- we are creating a Cell object assigning a int value from the "input" and then Assigning the Cell to board[i][j] (this is the 9x9 Sudoku board) .
+display() function simply displays the Sudoku board.
+SudokuSolver(int i,int j) solves Sudoku using brute force technique.
+"if (board[i][j].canChange == false)" (line 104) Here we check if the number was given to us as a part of the problem or was it entered by us is any of the previous step.
+"if (firstChangableCell == 0)" (line 105) Here we check and store the co-ordinated of the number which appears before the 1st zero in the input sequence.
+ What we are doing is assigning a the 1st possible value to the cell( as er sudoku rules) then move to the next cell and do the same. If a cell could be assigned any value between 1 to 9, we go back to the previous cell and assign it the 2nd possible value and come back to the cell and try to assign it a value again... We perform this recursively.
+"check(int row, int col, int val)" (line 183) helps us determine if a value could be assigned or no.
+"goback" is used to tell the code whether to traverse back (to change previous value) or forward (to assign new value) on the Sudoku board.
+termination condition (line 145) . while traversing the board it eventually will reach the last point on the board ie board[8][8]. which will imply the board has been solved. and we can terminate the program.


I hope I have explained it well. I apologize in advance if you find the explanation ambiguous. I'll gladly reexplain the required section.

 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just ran your code, it gives me the following output, without an exception:
 
Mustafa Dasorwala
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is just amazing............... What you have got is the perfect ans. I ran the code using both Netbeans and eclipse, and I got the same error (i am still getting the same error i described). What determines the stack size? Is there a way to change it? Do these IDE's place some limitations on stack size? Thanks Stephan. I was really wondering whats wrong with the code logic. Happy to know there is no logical error.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never write == false or == true. Not only are those things poor style for if (b) and If (!b), but also they are error-prone because you might write = by mistake.
 
Mustafa Dasorwala
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Never write == false or == true. Not only are those things poor style for if (b) and If (!b), but also they are error-prone because you might write = by mistake.



Thanks. I will keep that is mind................
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Mustafa Dasorwala
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please throw some light on this stack overflow issue
 
Mustafa Dasorwala
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am still facing the same problem..........
How come you didn't face the same problem Stephan. How did you run the code.
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just copied it into NetBeans, and ran it.

Anyway, there's a lesson in this. Even if the program may run fine on one system, it may fail catastrophically on another.

In an imperative language like Java, it's wise to prefer iteration over recursion. Method calls are more expensive than looping statements, and the stack is relatively small.
 
Mustafa Dasorwala
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is what i did in eclipse to solve my problem.

1. Go to Run -> RunConfigurations...
2. Select Java Applications -> your_project
3. Select (x)=Arguments in the right panel.
4. Enter "-Xss1024k" (without the quotes) in VM Arguments box.
5. Apply changes and Run.

This increases the stack size and the code runs without and exceptions.
Thanks a lot Stephan for your time and support.
I'll mark this thread as solved. Any other valuable input or idea is always welcomed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic