Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Stack OverFlow Error

 
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • 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.
 
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
Netbeans IDE Oracle Java
  • 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.
 
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.
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic