“线程中的异常”主“java.lang.StackOverflowError”错误?

时间:2016-11-09 20:58:30

标签: java

当我尝试在main方法中调用另一个类的方法时,我遇到了该标题中描述的错误。错误指向我的类TestCalculator的第三行。这是代码:

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();

}   
}

我不太清楚为什么会出现这种错误。如果有人可以帮助我理解这个错误是什么以及它为什么会发生,那么我和其他任何可能在将来也会遇到这个问题的人都会非常感激。感谢。

2 个答案:

答案 0 :(得分: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 :(得分: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没有问题;在new TestCalculator()内调用TestCalculator没有问题。问题是在类的构造函数或实例初始值设定项中直接或间接无条件地调用new TestCalculator()

修复方法是从代码中删除TestCalculator c = new TestCalculator();行;或者,至少将其更改为TestCalculator c;(即保持未初始化状态)。