• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Having StackOverflowError on gui

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I got a StackOverflowError om my code and i don't know how to debug it.

Here's the error. The last four lines repeat until the end of the error.


Exception in thread "main" java.lang.StackOverflowError
at sun.awt.windows.WToolkit.getScreenInsets(Native Method)
at sun.awt.windows.WToolkit.getScreenInsets(Unknown Source)
at sun.java2d.SunGraphicsEnvironment.getUsableBounds(Unknown Source)
at java.awt.GraphicsEnvironment.getCenterPoint(Unknown Source)
at java.awt.Window.setLocationRelativeTo(Unknown Source)
at view.JanelaPrincipal.show(JanelaPrincipal.java:70)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at view.JanelaPrincipal.show(JanelaPrincipal.java:72)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at view.JanelaPrincipal.show(JanelaPrincipal.java:72)



My code:
Don't be alarmed if you see any portuguese word. Yes, i'm brazilian. I hope that it isn't a problem.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's how to go through and interpret the error message:

Exception in thread "main" java.lang.StackOverflowError
at sun.awt.windows.WToolkit.getScreenInsets(Native Method)
at sun.awt.windows.WToolkit.getScreenInsets(Unknown Source)
at sun.java2d.SunGraphicsEnvironment.getUsableBounds(Unknown Source)
at java.awt.GraphicsEnvironment.getCenterPoint(Unknown Source)
at java.awt.Window.setLocationRelativeTo(Unknown Source)
at view.JanelaPrincipal.show(JanelaPrincipal.java:70)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at view.JanelaPrincipal.show(JanelaPrincipal.java:72)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at view.JanelaPrincipal.show(JanelaPrincipal.java:72)


Stack overflow errors usually come from a method that calls itself and never returns.  Looking at the stack trace, you start at the top.  This line shows you the first place an error was detected.  It is often deep in some code you don't know.  The next line is the line of code that called the line above, that is, the code in line two called the code in line one.

Now scan down the stack trace.  Look for your program name.  It will be fully qualified, so the package name will be first.  I've highlighted the lines that contain your program's code.  Notice that first line 70 is called, but then line 72 is called from line 72...

So line 72 somehow is "calling itself."  From the stack trace just below your program line 72, you can see that the method show is being called.  But program line 72 of your program is in the method show!

So there is your problem.  Program line 72 sets visibility, which calls the show method, that sets visibility, that calls the show method...
 
Saloon Keeper
Posts: 15489
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Filipe, welcome to CodeRanch!

StackOverflowError typically occurs when you have a recursive loop without an end condition. In this case, it's probably caused because you're overriding the show() method defined in Window. setVisible() calls show() and show() calls setVisible(), and you end up with an infinite loop.

Your compiler should have given you a warning that you're overriding a deprecated method. Always fix all compiler warnings before you run the program.

A few tips for designing Swing forms:

  • Your fields should be final, and you should instantiate them in the constructor.
  • Lay out your form in a private initComponents() method that you call from the constructor.
  • Provide a separate method that calls pack() and setVisible(). These should never be called from the constructor.
  • Don't let your form extend JFrame. Instead, your form should contain a JFrame field.
  • Don't let your form implement any listeners. Instead, you should create anonymous instances of listeners and add them to your components directly.
  • Instead of using a WindowListener you can use a WindowAdapter to avoid declaring all the empty method stubs.
  • In this case you don't need a WindowListener at all, you can just call setDefaultClosingOperation(JFrame.DISPOSE_ON_CLOSE) on your frame.
  •  
    Filipe Soares
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ops, it's a very simple error :X
    Thanks for your help. I didn't paid attention that show is a Window's method
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15489
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here's a simple example form that illustrates some of the tips I gave earlier:
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic