Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;“线程中的异常”;“主要”;java.lang.StackOverflowerError“;错误?_Java - Fatal编程技术网

&引用;“线程中的异常”;“主要”;java.lang.StackOverflowerError“;错误?

&引用;“线程中的异常”;“主要”;java.lang.StackOverflowerError“;错误?,java,Java,当我试图从主方法中的另一个类调用方法时,我遇到了该标题中描述的错误。错误指向我的类TestCalculator的第三行。代码如下: 测试计算器类 public class TestCalculator { Double x; TestCalculator c = new TestCalculator(); String string = "b"; Double doubleObject = 1.0; double doublePrimitive = 2;

当我试图从主方法中的另一个类调用方法时,我遇到了该标题中描述的错误。错误指向我的类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();

}   
}
主类

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

}   
}

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

要解决此特定问题,请删除第3行,并删除代码中对
c
的任何引用。如果你说的是
c.x(doubleObject)
,你应该只使用
x(doubleObject)
。您正在构建的本身就是一个
TestCalculator
,因此无需在其中创建另一个
TestCalculator

这将修复您所遇到的错误,但它也会立即将您带到其他性质非常相似的错误

在一个非常基本的层面上,不要在函数本身中调用函数(比如在
x
中调用
x
)。这是一种称为递归的专门技术,在这里对您没有帮助。此外,不要为函数指定与传入参数相同的确切名称。我指的是这个而不是

public Double x(String x)
你可以用类似

public Double choose(String command)
否则,您将混淆
x
的两种不同用法


在像choose这样的函数中,您必须使用提供的字符串,
命令
,并使用if语句和Java的字符串函数来确定
命令
需要什么。

您可以将
TestCalculator
类提取为以下内容:,这仍然给出了
堆栈溢出错误
(这就是a的意思):

编译
TestCalculator
时,编译器会将其转换为如下内容:

public class TestCalculator {
  TestCalculator c;

  public TestCalculator() {
    c = new TestCalculator();
  }
}
想想会发生什么:在
TestCalculator
的构造函数中,您正在创建一个
TestCalculator
的新实例,它将调用
TestCalculator
的构造函数

但是在调用
TestCalculator
的构造函数时,您创建了
TestCalculator
的一个新实例,它将调用
TestCalculator
的构造函数

在调用
TestCalculator
的构造函数时,您将创建一个
TestCalculator
的新实例,该实例将调用
TestCalculator
的构造函数

等等。如果您创建一个
TestCalculator
的实例,您只需继续创建
TestCalculator
的实例,每次在堆栈上再推一帧。最后,堆栈上的空间用完,您会得到一个
stackoverflowerrror


TestCalculator
中引用
TestCalculator
没有问题;在
TestCalculator
内部调用
newtestcalculator()
也没有问题。问题是在类的构造函数或实例初始值设定项中直接或间接地无条件调用
newtestcalculator()


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

您正在一遍又一遍地调用函数,您希望得到什么结果?我不知道如何为每个方法赋值,然后在主方法中调用该方法。由于所有的方法都有相同的名称,这让我对如何区分这些方法感到困惑。你的意思是什么?现在没事了,它似乎起作用了。谢谢。给方法赋值吗?这根本没有任何意义!