• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.StackOverflowError

 
Sandy Abraham
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I wrote a class for MyCanvas which has the paint method.
And i have another class 'HandleCanvas' which extends MyCanvas and implements KeyListener. In MyCanvas i wrote addKeyListener(new HandleCamvas()). Everything compiles ok. except that when i actually execute it, there's this java.lang.StackOverflowError in main. From what i know, it's because of some recursive method calling. But I am not sure where went wrong. Please help. Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you cut-and-pasted the stack trace here, we could probably tell you, but without it, we'd just be guessing.
 
VIJAY Yadlapati
Ranch Hand
Posts: 175
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(1) HandleCanvas extends MyCanvas implements KeyListener
(2) MyCanvas -> addKeyListener(new HandleCamvas());
When (2) is called, you have to create a new instance of HandleCanvas - which intern makes a call to its super class constructor, MyCanvas().
And I guess, you would have added the listener (2) in the constructor of MyCanvas.
So, you have to create another instance of HandleCanvas for adding keylistener - and the process continues.. until stack gets full.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic