简体   繁体   中英

Need help figuring out why I am getting a java.lang.StackOverflowError on my TestClock.java program

Hello and thanks for any help you can offer,

I am still pretty new to Java and I need some help figuring out why my program doesn't work. Everything looks good when I compile and I used two command line args (11:45:12 11:48:13). When I run the program it kicks back this error:

Exception in thread "main" java.lang.StackOverflowError at Clock.toString(Clock.java:37)

What am i forgetting to do? Any idea what i need to fix?

Here is the code:

For my Clock Class:

//header files

import java.time.LocalTime;
import static java.lang.System.out;

// creating class clock
public class Clock {

// private data fields
private LocalTime startTime;
private LocalTime stopTime;

// no argument cosntructor to initilize startTime to current time
protected Clock() {
    startTime = LocalTime.now();
}

//method start() resets the startTime to the given time
protected LocalTime start() {
    startTime = LocalTime.now();
    return startTime;
}

//method stop() sets the endTime to given time
protected LocalTime stop() {
    stopTime = LocalTime.now();
    return stopTime;
}

//getElapsedTime() method returns elapsed time in sconds
private void geElapsedTime() {
    long elapsedTime = stopTime.getSecond() - startTime.getSecond();
    out.println("Elapsed time is seconds: " + elapsedTime);
}

public String toString() {
    return toString();
}
}

For my TestClock Class:

// header files
import java.time.LocalTime;
import static java.lang.System.err;
import static java.lang.System.out;

// creating class of TestClock
class TestClock {

// construct a clock instance and return elapsed time
public static void main(String[] args) {

// creating object
    Clock newClock = new Clock();

// checking the condition using loop
    if (args.length == 2) {
        LocalTime startTime = LocalTime.parse(args[0]);
        LocalTime endTime = LocalTime.parse(args[1]);
    }
    else {
        err.println("Application requires 2 command line arguments");
                  System.exit(1);
    }

// display new clock value
    out.println(newClock);

}


}

Your toString() method in the Clock class is recursively calling itself. I think you probably wanted super.toString() , although, in that case overriding the method in the first place is unnecessary. If you wanted to print the times, you would use startTime.toString() or stopTime.toString() .

You are returning toString() to itself basically, which looks to me to be a recursive call. This is over-flowing the stack and giving you the error. toString() is a method in the Object class, which all objects inherit from. You need to over-ride it with your own String interpretation. You should do something like

@Override
public String toString()
{
return "The starttime is: " + startTime " and endtime is: " + endTime";
}

Your toString method calls itself infinitely. You should just remove it,especially since it does not output anything special.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM