Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

how to solve this type of stackoverflowerror...

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a java program which runs repeatedly by calling to main functions in a loop based on a condition ..But after running for some time(say 30 minutes),it shows the follwing error and it terminate the program execution.But as per my requirement i want to run this program for several hours or time without showing any error and terminating the program.

Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.StreamEncoder$CharsetSE.implWrite(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at pitstop.automation1.main(automation1.java:18)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)
at pitstop.automation1.main(automation1.java:298)


Could anyone please tell me what is the exact reason for this and how to fix this error.

The code for the same program is below:



Here i am calling the main function again and again using automation1.main(args) at line no 299 and 304.

Please help me in this issue.


Thanks,
Sanat
>
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The calls to main() don't appear to be in any "loop based on a condition" -- every time main() gets to the end, it calls main() again, so eventually, you're going to get a stack overflow.

Instead of calling main() from main(), why don't you move the whole body of main() to another routine (called, say, doWhateverTheHeckThisProgramDoes()) and then in main(), just have a loop like



The recursion goes away, and you don't get any stack overflows.

P.S. "sleep()" is a static method of Thread; you can just call Thread.sleep(), without creating a THread object.
 
sanat meher
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got your idea.but dont understand how to implement it.Here calling to the main() is part of the body of the main() function itself.So how can we call the routine from main().
Please specify the code for the same.

Regards,
Sanat
 
Marshal
Posts: 70674
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sanat meher wrote: . . . Here calling to the main() is part of the body of the main() function itself.So how can we call the routine from main(). . . .

You don't. You will have to implement it differently.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sanat meher wrote:i got your idea.but dont understand how to implement it.Here calling to the main() is part of the body of the main() function itself.So how can we call the routine from main().



As I said in my post, move almost all of main() into another routine, and then just have the loop in main(). What more do you need?
 
reply
    Bookmark Topic Watch Topic
  • New Topic