2016-11-09 89 views
-2

當我試圖在我的主要方法中調用另一個類的方法時,我遇到了該標題中描述的錯誤。該錯誤指向我的類TestCalculator的第三行。下面是代碼:「線程異常」main「java.lang.StackOverflowError」錯誤?

TestCalculator類

public class TestCalculator { 
     Double x; 
     TestCalculator c = new TestCalculator(); 
String string = "b"; 
Double doubleObject = 1.0; 
double doublePrimitive = 2; 


     /* 
     * Chops up input on ' ' then decides whether to add or multiply. 
     * If the string does not contain a valid format returns null. 
     */ 
     public Double x(String x){ 
      x("12 [ 3"); 
       return new Double(0); 
     } 
     public void testParsing() { 

     if (c.x(doubleObject) == 17) { 
      System.out.println("Adding Success");} 
      else { 
        System.out.println("Adding Fail"); 
        } 
     if (c.x(doublePrimitive) == 60) { 
      System.out.println("Multiplying Success");} 
      else { 
        System.out.println("Multiplying Fail"); 
        } 
     if (c.x(string) == null) { 
      System.out.println("Valid operator Success");} 
      else { 
        System.out.println("Valid operator Fail"); 
        } 
     } 
     /* 
     * Adds the parameter x to the instance variable x and returns the answer as a Double. 
     */ 
     public Double x(Double x){ 
       System.out.println("== Adding =="); 
       x("12 + 5"); 

       return new Double(0); 
     } 
     /* 
     * Multiplies the parameter x by instance variable x and return the value as a Double. 
     */ 
     public Double x(double x){ 
       System.out.println("== Multiplying =="); 
       x("12 x 5"); 

       return new Double(0); 
     } 
} 

主類

public class Main { 

public static void main(String[] args) { 

TestCalculator call = new TestCalculator(); 
call.testParsing(); 

} 
} 

我也不太清楚,爲什麼這個錯誤發生。如果有人能幫助我理解這個錯誤是什麼以及它發生的原因,那麼我自己和其他可能在未來也會遇到這個問題的人將會非常感激。謝謝。

+0

你一遍又一遍地調用函數沒有結束,你期望什麼結果? – Li357

+0

我很困惑如何爲每個方法賦值,然後在主方法中調用該方法。由於所有方法都有相同的名稱,這使我很難理解如何區分這些方法。 – user7128699

+0

你是什麼意思? – Li357

回答

1

要解決此特定問題,請刪除第3行,並在代碼中刪除對c的任何引用。你在說什麼,如c.x(doubleObject),你應該只使用x(doubleObject)。你正在建造的是它自己,一個TestCalculator,所以沒有必要在內部創建另一個TestCalculator

這將修復您遇到的錯誤,但它也會立即將您帶到本質上非常相似的其他錯誤。

在一個非常基本的層面上,不要在其內部調用函數(如在x內調用x)。這是一種稱爲遞歸的專門技術,它不會幫助您。另外,不要給你的功能完全相同的名稱作爲傳遞的參數。通過這個,我的意思是不是

public Double x(String x) 

你可以使用這樣的

public Double choose(String command) 

否則,你會在x的兩種不同用途之間混淆。

在類似choose的函數中,您必須使用提供的字符串command,並使用if語句和Java的字符串函數來確定command需要什麼。

1

您可以提煉您TestCalculator節課下來以下,仍然給出了StackOverflowError(這是由Minimal, Complete, and Verifiable example的意思):

public class TestCalculator { 
    TestCalculator c = new TestCalculator(); 
} 

public class Main { 
    public static void main(String[] args) { 
    new TestCalculator(); 
    } 
} 

當你編譯TestCalculator,編譯器將其轉換爲類似以下內容:

public class TestCalculator { 
    TestCalculator c; 

    public TestCalculator() { 
    c = new TestCalculator(); 
    } 
} 

想想發生的事情:在TestCalculator構造函數中,你要創建的TestCalculator一個新實例,這將調用TestCalculator的構造函數。

但是在調用構造函數TestCalculator時,創建了一個新的實例TestCalculator,它將調用TestCalculator的構造函數。

而在調用的TestCalculator構造函數中,您創建的TestCalculator一個新實例,調用的TestCalculator構造....

等等等等等等。如果你創建的TestCalculator一個實例,你只是不停在創建TestCalculator的實例時,每次將多個幀推入堆棧。最終,您在堆棧中的空間不足,並且您獲得StackOverflowError


沒有與具有與TestCalculator內部TestCalculator參考沒問題;並且在TestCalculator內部調用new TestCalculator()沒有問題。該問題無條件地直接或間接地在類的構造函數或實例初始值設定項中調用new TestCalculator()

修復方法是從您的代碼中刪除TestCalculator c = new TestCalculator();行;或者至少將其更改爲TestCalculator c;(即將其保留爲未初始化)。

相關問題