2013-01-31 41 views
0

我的程序使用BufferedWriter封裝在一個FileWriter中,每次運行它時都會在某個地方結束。我不能爲了我的生活找出原因。Java程序隨機結束,沒有錯誤

以下是我的代碼。我曾經思考過一次內存泄漏,但我確定會拋出一個異常(當然,除非我已經阻止它顯示出來)。如果它應該拋出一個錯誤,我該如何解決它? 感謝

import java.io.*; 

public class BuffProcess { 

static BufferedReader brOne, brTwo; 
static FileReader inOne, inTwo; 
//static FileInputStream fOne, fTwo; 
static FileWriter out; 
static BufferedWriter bw; 
static StringBuffer sbOne, sbTwo; 
static String stOne, stTwo, tech; 
static char cOne, cTwo; 
static boolean bOne, bTwo; 
static int count, intOne, intTwo, intThree, intFour, intFive, intSix, maxSet, lineCount; 
static int pCount, check; 

public static void p(String s) { 

    try { 
     inOne = new FileReader(s + ".ck2"); 
     inTwo = new FileReader(s + ".ck2"); 
     brOne = new BufferedReader(inOne); 
     brTwo = new BufferedReader(inTwo); 
     out = new FileWriter(s + "_edit.ck2"); 
     bw = new BufferedWriter(out); 
    } catch (FileNotFoundException e) { 
     System.out.println("FileNotFoundException"); 
    } catch (IOException e) { 
     System.out.println("IOException"); 
    } 

    stOne = "Start"; 
    stTwo = "Start"; 
    tech = "Pants"; 
    bOne = true; 
    bTwo = false; 
    count = 0; 
    pCount = 929; 

    try { 

     while (stOne != null) { //For entire file 
      lineCount++; 
      //print("Lines processed=" + lineCount); 
      stOne = brOne.readLine(); 
      brTwo.readLine(); 
      bw.write(stOne); bw.newLine(); 

      // Find settlements area of file 
      if (stOne.contains("name=\"Vestisland\"")) { //Vestisland action (settlements section) 

       for (int i = 2; i > 0; i--) { 
        stOne = brOne.readLine(); 
        brTwo.readLine(); 
        bw.write(stOne); bw.newLine(); 

       } 

       stOne = brOne.readLine(); 
       brTwo.readLine(); 

       if (stOne.contains("max_settlements=")) {//For Vestisland only 

        while (bOne) { //Find tech levels 
         stTwo = brTwo.readLine(); 
         count++; 

         if (stTwo.contains("technology=")) { 

          for (int i = 4; i > 0; i--) { 
           stTwo = brTwo.readLine(); 
           count++; 
          } 
          tech = stTwo; 
          bOne = false; 
         } 
        } 
        bOne = true; 
        maxSet(); 
        bw.write(maxSetBlank + maxSet + "\n"); bw.newLine(); 
        pCount--; 
        //print("Number of provinces remaining=" + pCount); 
        //print("Number of provinces done=" + (929 - pCount)); 
        print("Vestisland" + ++check); 

        while (count !=0) { 
         brOne.readLine(); 

         count--; 
        } 
       } 
       bw.flush(); 
       out.flush(); 

       while (pCount > 0) {// For provinces section 
        stOne = brOne.readLine(); 
        //print(stOne); 
        brTwo.readLine(); 
        bw.write(stOne); bw.newLine(); 

        if (stOne.contains("name=")) { 
         names: 
         switch (stOne) { 

         case nullOne: case nullTwo: case nullThree: case nullFour: case nullFive: 
          print(test + "null"); 
          pCount--; 
          //print("Number of provinces remaining=" + pCount); 
          //print("Number of provinces done=" + (929 - pCount)); 
          print("Desert" + ++check); 
          break names; 

         default: 

          for (int i = 2; i > 0; i--) { 
           stOne = brOne.readLine(); 
           brTwo.readLine(); 
           bw.write(stOne); bw.newLine(); 
          } 

          stOne = brOne.readLine(); print(stOne); 
          brTwo.readLine(); 

          if (!stOne.contains("max_settlements")) { 

           while (bOne) { //Find tech levels 
            stTwo = brTwo.readLine(); 
            count++; 

            if (stTwo.contains("technology=")) { 

             for (int i = 4; i > 0; i--) { 
              stTwo = brTwo.readLine(); 
              count++; 
             } 
             tech = stTwo; 
             bOne = false; 
            } 
           } //Find tech levels 
           bOne = true; 
           maxSet(); 
           bw.write(maxSetBlank + maxSet + "\n"); bw.newLine(); 
           bw.write(stOne); bw.newLine(); 
           pCount--; 
           //print("Number of provinces remaining=" + pCount); 
           //print("Number of provinces done=" + (929 - pCount)); 
           print("Without max_settlements" + ++check); 
          } else { 

           while (bOne) { //Find tech levels 
            stTwo = brTwo.readLine(); 
            //print(stTwo); 
            count++; 

            if (stTwo.contains("technology=")) { 

             for (int i = 4; i > 0; i--) { 
              stTwo = brTwo.readLine(); 
              //print(stTwo); 
              count++; 
             } 
             tech = stTwo; 
             bOne = false; 
            } 
           } 
           bOne = true; 
           maxSet(); 
           bw.write(maxSetBlank + maxSet + "\n"); bw.newLine(); 
           pCount--; 
           //print("Number of provinces remaining=" + pCount); 
           //print("Number of provinces done=" + (929 - pCount)); 
           print("With max_settlements" + ++check); 
          } 
          break names; 
         } 
       } 

      } //While provinces remain 

       try { 
        while (stOne != null) { 
         bw.write(brOne.readLine()); bw.newLine(); 
        } 
       } catch (NullPointerException e) { 
        stOne = null; 
       } 

     } 
     } 
    } catch (IOException e) { 
     System.out.println("IOException"); 
    } catch (NullPointerException e) { 

    } finally { 
     try { bw.close(); } catch (IOException e) { 
      System.out.println("IOException"); 
     } finally { } 
    } 
    System.out.println("Provinces=" + pCount); 
} 

private static void maxSet() { 
    int avg; 
    intOne = Integer.parseInt(tech.substring(16, 17)); 
    intTwo = Integer.parseInt(tech.substring(18, 19)); 
    intThree = Integer.parseInt(tech.substring(22, 23)); 
    intFour = Integer.parseInt(tech.substring(24, 25)); 
    intFive = Integer.parseInt(tech.substring(30, 31)); 
    intSix = Integer.parseInt(tech.substring(46, 47)); 
    avg = Math.round((intOne+intTwo+intThree+intFour+intFive+intSix)/6); 
    maxSet = avg + 2; 
    //print(maxSet); 
} 

private static void print(Object o) { 
    System.out.println(o); 
} 

static int pCheck = 0; 
static String test = "This line is reached"; 
static final String maxSetBlank = "\t\tmax_settlements="; 
static final String nullOne = "\t\tname=\"Western Sahara\"\n"; 
static final String nullTwo = "\t\tname=\"Adrar Plateau\"\n"; 
static final String nullThree = "\t\tname=\"Siberia\"\n"; 
static final String nullFour = "\t\tname=\"Sahara Desert\"\n"; 
static final String nullFive = "\t\tname=\"Sharawrah\"\n"; 

}

+0

它在哪裏意外結束? – iamnotmaynard

+0

您是否在使用像eclipse或netbeans這樣的IDE?在調試器中運行。 – OldProgrammer

+0

在您的第一個catch塊中,您甚至不退出程序... – fge

回答

3

我懷疑它拋出,你都忽略一個NullPointerException。

我會建議刪除所有你的異常捕捉,讓方法throws這些例外。如果你打算對他們做一些有用的事情,那麼只有值得注意的例外。

順便說一句:我懷疑你的所有字段都不需要是靜態的,如果可以的話,我會創建局部變量。

0

您接收到底部附近的NullPointerException不會打印任何內容。

建議的言辭...使用一個自動記錄錯誤的IDE,如Eclipse。它會使你的生活容易十億倍。

+0

我正在使用Eclipse,但是在它試圖將null分配給String(這是我的while循環的條件)的末尾有一個NullPointerException,並且它不起作用,所以while循環永遠不會結束。我使用NullPointerException將null分配給String。無法真正想到其他方式。 –

0

一開始,打印堆棧跟蹤你的catch塊,例如,通過

} catch (IOException e) { 
    e.printStackTrace(); 
} 

更換

} catch (IOException e) { 
    System.out.println("IOException"); 
} 

,避免不重新拋出或登錄他們捕捉異常。調試過程中您目前遇到的問題是後果。順便說一句,如果你不知道如何處理一個異常點,那麼不要抓住它。例如,IOException一般不應被捕獲和忽略。

也不要捕捉邏輯錯誤,如NullpointerException,IllegalStateException,IllegalArgumentExceptionAssertionError。這幾乎總是編程錯誤。當你抓住它們並忽略它們時,你隱藏了這個問題,但是你還沒有解決它。