2016-11-09 87 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;(即将其保留为未初始化)。

相关问题