What is java.lang.StackOverFlow error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What is java.lang.StackOverFlow error?

I tried to prepare a program using Threads but unfortunately it crashes with the StackOverFlow error.I can't understand that how can I resolve this error. Please help me so that I can complete my program. The code is below:- https://code.sololearn.com/c0j3SWnXKlnc/?ref=app

8th Mar 2018, 9:39 AM
Sujal Kumar
Sujal Kumar - avatar
4 Answers
+ 3
You are creating a new instance of a customer class from the sl class, and then creating a new instance of the sl class in that customer class. This is recursive, and a recursive function with no base case results in a stack overflow exception. You should only instantiate one of each class as public, then reference them to each other.
8th Mar 2018, 10:11 AM
apex137
apex137 - avatar
+ 3
Thanks for the suggestion. You mean that I have to create a different public class that will run all the methods and initiate all the classes.
8th Mar 2018, 10:52 AM
Sujal Kumar
Sujal Kumar - avatar
+ 3
That's not exactly what I meant. I suggest doing something like public sl sl1 = new sl(), then referencing only to sl1 throughout the entire program, and not using any new instances of that class anymore.
8th Mar 2018, 11:17 AM
apex137
apex137 - avatar
+ 1
OK Thank You very much.
9th Mar 2018, 4:34 AM
Sujal Kumar
Sujal Kumar - avatar