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

exception at runtime  RSS feed

 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why does it produce an exception at runtime??? Help?

EJavaGuruPrivate2.main({ });
    Exception occurred.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor in Course creates a new instance of Course (line 4) which results in the constructor being called. Then...
The constructor in Course creates a new instance of Course (line 4) which results in the constructor being called. Then...
The constructor in Course creates a new instance of Course (line 4) which results in the constructor being called. Then...
The constructor in Course creates a new instance of Course (line 4) which results in the constructor being called. Then...
The constructor in Course creates a new instance of Course (line 4) which results in the constructor being called. Then...
The constructor in Course creates a new instance of Course (line 4) which results in the constructor being called. Then...
The constructor in Course creates a new instance of Course (line 4) which results in the constructor being called. Then...
The constructor in Course creates a new instance of Course (line 4) which results in the constructor being called. Then...
The constructor in Course creates a new instance of Course (line 4) which results in the constructor being called. Then...
The constructor in Course creates a new instance of Course (line 4) which results in the constructor being called. Then...
The constructor in Course creates a new instance of Course (line 4) which results in the constructor being called. Then...
...

Which leads to StackOverflowError.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it often helps if you post the exact error - or at least, th beginning of it.  When I run this code, I get this:

C:\slop>java EJavaGuruPrivate2
Exception in thread "main" java.lang.StackOverflowError
        at Course.<init>(Course.java:4)
        at Course.<init>(Course.java:4)
        at Course.<init>(Course.java:4)
        at Course.<init>(Course.java:4)
        at Course.<init>(Course.java:4)
        at Course.<init>(Course.java:4)

It is possible your settings don't let you see the beginning of the errors.  If you are using Windows, the cmd window by default only has bout 100 lines of buffer. You can right-click the title bar on it, go to 'properties', and change the "Screen Buffer Size" height to something bigger. I have mine set to 9999.

Once you do that, you can scroll up and see the beginning of the error message. That first line tells you exactly what the problem is, once you learn how to interpret it. "StackOverflowError" means you've called too many methods inside each other. Almost always it means you have a method calling itself, either directly or indirectly. As Pawel pointed out, your constructor for your Course class creates a new Course object, which calls the constructor...

edit - if you are not using the cmd window to compile and run your code, I would suggest you start doing so. In the early stages of programming, it really is helpful to see what's going on and helps you learn some of subtleties of the language.
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it, thanks !
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!