• Post Reply Bookmark Topic Watch Topic
  • New Topic

exception at runtime  RSS feed

 
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.

 
Bartender
Posts: 2123
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.
 
lowercase baba
Bartender
Posts: 12592
50
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 !
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
The WEB SERVICES and JAX-RS Course
https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!