September 04, 2010, 03:35:38 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: how to solve this type of stackoverflowerror...  (Read 1556 times)
0 Members and 1 Guest are viewing this topic.
JavaLearner
Member
*

Reputation: 0
Offline Offline
Posts: 34
Referrals: 0

Awards
« on: July 11, 2009, 08:46:18 AM »

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(Unkno wn 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:
   
Code
GeSHi (java):
  1. package pitstop;
  2.  
  3. import java.io.*;  
  4. import java.util.*;  
  5. import java.io.Closeable;
  6. import java.io.DataInputStream;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.FilenameFilter;
  11. import java.io.IOException;
  12.  
  13.  
  14. public class automation1 {
  15.  
  16. public static void main(String[] args) throws InterruptedException {  
  17.  
  18. System.out.println("--");
  19. String str3=null;
  20. String fNameOne = "D:\\textfile\\";
  21. File myDir=new File(fNameOne);
  22. //System.out.println("myDir:" +myDir);
  23.     FilenameFilter select=new FileListFilter("","txt");
  24.     File[] content=myDir.listFiles(select);
  25.     String  filepath=null;
  26.     String fName=null;
  27.     String fileName = null;
  28.     String fileName1=null;
  29.        File dir = new File(fNameOne);
  30.        String[] chld = dir.list();
  31.       // System.out.println("dir length:" +chld.length );
  32.        for(int i = 0; i < chld.length; i++){
  33.             fileName = chld[i];
  34.            System.out.println("Filename:"+fileName);
  35.        }
  36.  
  37.        Thread t=new Thread();
  38.  
  39. t.sleep(1000);
  40.  
  41.  
  42.     //if(content!=null){
  43. for(int i = 0; i < chld.length; i++){
  44.  
  45. fileName1 = chld[i];
  46. System.out.println("Fname:"+fNameOne);
  47. System.out.println("Filename1:"+fileName1);
  48.          //  switch(1){
  49.           // case 1:{
  50.  
  51.  
  52.  
  53.             fName = fNameOne+fileName1;
  54.             System.out.println("Fname1:" +fName);
  55.             String thisLine; //string variable which take each line at a time  
  56.             FileInputStream fis = null;  
  57.  
  58.             try {
  59.  
  60.                    fis = new FileInputStream(fName);  
  61.  
  62.                } catch (FileNotFoundException e) {  
  63.                    // TODO Auto-generated catch block  
  64.                  //  e.printStackTrace();  
  65.                }
  66.  
  67.  
  68.             DataInputStream in = new DataInputStream(fis);
  69.                BufferedReader br = new BufferedReader(new InputStreamReader(in));
  70.                String strLine = null;
  71.                String replacewith="\\";
  72.                String strLine1=null;
  73.  
  74.                //Read File Line By Line
  75.               // while (strLine  != null)   {
  76.                try {
  77.                 while ((strLine = br.readLine()) != null)   {
  78.  
  79. //while ((thisLine = in.readLine()) != null)  
  80.  // Print the content on the console
  81.  System.out.println (strLine);
  82.  String str=strLine.substring(0,1);
  83.  String str1=strLine.replaceFirst(str,"" );
  84.  String str2=str1.replaceFirst("/", ":\\\\");
  85.   str3=str2.replaceAll("/","\\\\");
  86.  System.out.println ("str11:" +str3);
  87. /*  strLine=strLine.replaceAll("/d","D:");
  88.  System.out.println ("str:" +strLine);
  89. strLine1=strLine.replaceAll("/","\\\\" );
  90. System.out.println ("str11:" +strLine1);
  91. */
  92.    }
  93.                // System.out.println("str111" +strLine1);
  94.                 File f=new File(str3);
  95.                 String filename=f.getName();
  96.                 filepath=f.getParent();
  97.                 System.out.println("filepath:" +filepath);
  98.                 InputStream oInStream = new FileInputStream(str3);
  99.                    OutputStream oOutStream = new FileOutputStream("D:\\pitstop\\in\\" +filename);
  100.  
  101.                    // Transfer bytes from in to out
  102.                    byte[] oBytes = new byte[1024];
  103.                    int nLength;
  104.                    BufferedInputStream oBuffInputStream = new BufferedInputStream( oInStream );
  105.                    while ((nLength = oBuffInputStream.read(oBytes)) > 0)
  106.                    {
  107.                            oOutStream.write(oBytes, 0, nLength);
  108.                    }
  109.                    oInStream.close();
  110.                    oOutStream.close();
  111.  
  112.                    in.close();
  113.                 } catch (IOException e) {
  114. // TODO Auto-generated catch block
  115. e.printStackTrace();
  116. }
  117.              //  }
  118.                //Close the input stream
  119.                 Thread t1=new Thread();
  120.  
  121.             t1.sleep(1000);
  122.  
  123.                 System.out.println("FileName1111:" +fName);
  124.                   File ff=new File(fName);
  125.                   System.out.println("ff" +ff);
  126.                     boolean s=ff.delete();
  127.                     System.out.println("s" +s);
  128.                     if (!s) {
  129.                     System.out.println("File not deleted");
  130.                        // File was not successfully moved
  131.                     }        
  132.  
  133.  
  134.            String fName1 = "D:\\pitstop\\out";
  135.     File myDir1=new File(fName1);
  136.     //System.out.println("myDir:" +myDir);
  137.         FilenameFilter select1=new FileListFilter("","pdf");
  138.         File[] content1=myDir1.listFiles(select1);
  139.  
  140.         File dir4 = new File(fName1);
  141.            String[] chld4 = dir4.list();
  142.         System.out.println("chld4:" +chld4.length);
  143.  
  144.  
  145.  
  146.         if(chld4.length==0){
  147.  
  148.         String fName2 = "D:\\checkpdf\\pass\\";
  149.         File myDir2=new File(fName2);
  150.         //System.out.println("myDir:" +myDir);
  151.             FilenameFilter select2=new FileListFilter("","pdf");
  152.             File[] content2=myDir2.listFiles(select2);
  153.  
  154.  
  155.             String fileName3 = null;
  156.                File dir3 = new File(fName2);
  157.                String[] chld1 = dir3.list();
  158.               // System.out.println("dir length:" +chld.length );
  159.                for(int j = 0; j < chld1.length; j++){
  160.                     fileName3 = chld1[j];
  161.                   // System.out.println("Filename3:" +fileName3);
  162.                }
  163.               // System.out.println("content2:" +content2);
  164.  
  165.                String fName31 = "D:\\checkpdf\\fail\\";
  166.         File myDir31=new File(fName31);
  167.         //System.out.println("myDir:" +myDir);
  168.             FilenameFilter select31=new FileListFilter("","pdf");
  169.             File[] content4=myDir31.listFiles(select31);
  170.             System.out.println("content4:" +content4);
  171.  
  172.             String fileName4 = null;
  173.                File dir51 = new File(fName31);
  174.                String[] chld2 = dir51.list();
  175.               // System.out.println("dir length:" +chld.length );
  176.                for(int k = 0; k < chld2.length; k++){
  177.                     fileName4 = chld2[k];
  178.                  // System.out.println("Filename in fail:" +fileName4);
  179.                }
  180.  
  181.                String fName5 = "D:\\checkpdf\\corrupt\\";
  182.         File myDir61=new File(fName5);
  183.         //System.out.println("myDir:" +myDir);
  184.             FilenameFilter select41=new FileListFilter("","pdf");
  185.             File[] content5=myDir61.listFiles(select41);
  186.  
  187.  
  188.             String fileName5 = null;
  189.                File dir61 = new File(fName5);
  190.                String[] chld5 = dir61.list();
  191.               // System.out.println("dir length:" +chld.length );
  192.                for(int l = 0; l < chld5.length; l++){
  193.                     fileName5 = chld5[l];
  194.                   // System.out.println("Filename:"+fileName);
  195.                }
  196.  
  197.  
  198.  
  199.         if(content2!=null){
  200.  
  201.         System.out.println("in pass");
  202.  
  203.         for(int a=0;a<chld1.length;a++){
  204.  
  205.         fileName3 = chld1[a];
  206.         File file1 = new File("D:\\checkpdf\\pass\\" +fileName3 );
  207.  
  208.         System.out.println("File name in pass:" +fileName3);
  209.  
  210.  
  211.            // Destination directory
  212.            File dir1 = new File(filepath);
  213.  
  214.            // Move file to new directory
  215.            boolean success = file1.renameTo(new File(dir1, file1.getName()));
  216.            if (!success) {
  217.             System.out.println("File not moved from pass");
  218.                // File was not successfully moved
  219.            }
  220.            else
  221.               System.out.println("File moved from pass");
  222.         }
  223.  
  224.  
  225.         }
  226.  
  227.  
  228.         if(content4!=null){
  229.  
  230.         System.out.println("in fail");
  231.         File file2 = new File("D:\\checkpdf\\fail\\" +fileName4 );
  232.  
  233.  
  234.         System.out.println("File name in fail:" +fileName3);
  235.  
  236.        // Destination directory
  237.        File dir7 = new File(filepath);
  238.  
  239.        // Move file to new directory
  240.        boolean success11 = file2.renameTo(new File(dir7, file2.getName()));
  241.        if (!success11) {
  242.  
  243.            // File was not successfully moved
  244.         System.out.println("File not moved from fail");
  245.        }
  246.       else
  247.         System.out.println("File moved from fail");
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.         }
  257.  
  258.         if(content5!=null){
  259.  
  260.         System.out.println("in corrupt");
  261.         File file3 = new File("D:\\checkpdf\\corrupt\\" +fileName5 );
  262.  
  263.         String readme="readme.txt";
  264.  
  265.  
  266.          // Destination directory
  267.          File dir8 = new File(filepath);
  268.  
  269.          // Move file to new directory
  270.          boolean success111 = file3.renameTo(new File(dir8, file3.getName()));
  271.          if (!success111) {
  272.           System.out.println("File not moved from corrupt");
  273.              // File was not successfully moved
  274.          }else{
  275.           System.out.println("File moved from corrupt");
  276.  
  277.  
  278.        try {
  279.          BufferedWriter out = new BufferedWriter(new FileWriter(filepath +"\\" +readme));
  280.          out.write("Invalid input file");
  281.          out.close();
  282.      } catch (IOException e) {
  283.      }
  284.  
  285.  
  286.         }}
  287.  
  288.         }
  289.  
  290.  
  291.        //    }
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.          }
  299. automation1.main(args);
  300.  
  301.  
  302.     if(chld.length==0)
  303.     {
  304.     automation1.main(args);
  305.  
  306.     }
  307.            }
  308. }
  309.  
  310.  
Created by GeSHI 1.0.7.20

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.
Logged
Javaforums.net :: a community about Java software development.
« on: July 11, 2009, 08:46:18 AM »

 Logged
trey
Member
*

Reputation: 0
Offline Offline
Posts: 16
Referrals: 0

Awards
« Reply #1 on: December 07, 2009, 08:54:54 PM »

You are overflowing the call stack (which only has a limited amount of memory) with infinite recursion (when a method calls its self).
Logged
Javaforums.net :: a community about Java software development.
   

 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here
Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.635 seconds with 33 queries.

Google visited last this page September 01, 2010, 06:35:18 PM