[Logo]
Forums Register Login
exception at runtime


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

EJavaGuruPrivate2.main({ });
    Exception occurred.
 
(1 like)
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.
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.
got it, thanks !
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Nov 18, 2017 11:12:27.