This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stack OverFlow Error  RSS feed

 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please have a look at the following code

TestClass.java




TestBar.java




In this code, the "TestBar" class is a JavaBean, which extends JPanel and used to show JProgressBar. It is displayed inside the "TestClass" class. This is how this code should work

1. When I click the "play" button, the "add" button will be disabled
2. Then, the Timer in "TestBar" class will be invoked, and the JProgressBar will start working.
3. When the JProgressBar reaches its highest value, it will re-enable the "add" in the "TestClass" class using the method "addEnable()" which is in the "TestClass" class.

But, this code won't compile because it throws stack overflow error. But, when I remove the object of "TestClass" in the "TestBar" class (which is used to invoke the addEnable() method) the code gets compiled, and start to show the progress bar work. Anyway, I really need to re-enable the add button in the "TestClass" class, but I can't do it because of this error. Please help me.
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To clarify a little wording, your code compiles fine (I assume). You get the StackOverflowError when you run the code.

There's an infinite loop in your code. You can't instantiate a TestClass without creating a TestBar, which creates a TestClass, which creates a TestBar....and so on. Your solution is to remove the infinite loop, but still ensure the TestClass in TestBar gets instantiated (just not in the constructor!). You may want the TestBar instance to use the same TestClass object as the TestClass that created it?
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tina Smith wrote:To clarify a little wording, your code compiles fine (I assume). You get the StackOverflowError when you run the code.

There's an infinite loop in your code. You can't instantiate a TestClass without creating a TestBar, which creates a TestClass, which creates a TestBar....and so on. Your solution is to remove the infinite loop, but still ensure the TestClass in TestBar gets instantiated (just not in the constructor!). You may want the TestBar instance to use the same TestClass object as the TestClass that created it?


Thanks for the reply.

Yeah, sorry, that is a run time error. Anyway I can't take it to the same "TestClass" class because that javabean "TestBar" class will contain lot of business methods in my original application. If I take that to the same class, it will be a mess. Please help.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TestClass is a JFrame. Surely, if TestBar needs a reference to a TestClass, it needs a reference to the one it's contained in? In which case, pass it a reference. But it doesn't make sense to create a new JFrame inside the TestBar.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!