aspose file tools*
The moose likes Java in General and the fly likes how to solve this type of stackoverflowerror... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to solve this type of stackoverflowerror..." Watch "how to solve this type of stackoverflowerror..." New topic
Author

how to solve this type of stackoverflowerror...

sanat meher
Greenhorn

Joined: Nov 06, 2008
Posts: 18
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
>
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24185
    
  34

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.


[Jess in Action][AskingGoodQuestions]
sanat meher
Greenhorn

Joined: Nov 06, 2008
Posts: 18
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38898
    
  23
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
Marshal

Joined: Jul 08, 2003
Posts: 24185
    
  34

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?
 
wood burning stoves
 
subject: how to solve this type of stackoverflowerror...