This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello everyone, This is a little strange problem. I've written some source code in Java using Eclipse and I am running it thro' Eclipse. When and if a NullPointerException is thrown, Exception log gives all information about the line which threw the Exception and all. Now I create a Jar file of my compiled files and put this jar into the classpath, But now if there is NullPointerException, then all it says is NullPointerException <<CLASS-NAME.METHOD-NAME>> (unknownSource), when run. Can anyone suggest any idea of how to print the exact line no. which threw the Exception.
Thanks a lot for the great help. Regards, Varun Narang.
Your computer system is like AC, it's of no use when you open Windows ;)
(Unknown Source) comes from VMs that perform "tail call optimisation". This excludes the Sun VM, so I'll assume you're using the IBM VM. If I remember rightly, there is a switch to the VM to prevent this optimisation. The -g option has nothing to do with anything so you can ignore that little red herring.
 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4726340 Many will claim that Java will die without this RFE filled and I almost agree (I believe it will die anyway, but without this RFE, it will die sooner). This prediction is based on trends within the industry i.e. the invention of functional programming in Java which doesn't cater well for it. You can read about it On the flip side, the .NET VM includes a tail call instruction (and thus, tail call elimination), which allows "dysfunctional" (impure) languages to compile easily to the platform such as F# (an ML-based impure FP language).
 For fun, try this code on Sun VM:
It fails with stack overflow. Now run it on the IBM VM. It will probably run forever - if it fails, just run it again until eventually (usually immediately or soon thereafter) the VM will optimise the tail call in which case you are in an infinite loop with constant stack size. This is where your "Unknown Source" comes from - the tail call has been eliminated.
 ..since last working for The Filth on the IBM VM last year and seldom touching Java since. [ December 08, 2006: Message edited by: Tony Morris ]
Sorry, just realised it was the beginner's forum - excuse my excessive rant if you don't understand it.
"Unknown Source" comes from VMs that perform "tail call elimination" (this generally means IBM and not Sun). Find the switch to turn it off and run again - you should now have a more meaningful stack trace.
Jim's advice about needing to use -g is out of date, but his explanation isn't. You get "Unknown Source" whenever a class file doesn't include line number tables, as we get with the -g:none switch. There are doubtless various circumstances that can give rise to this, including those that Tony mentions -- but Occam would prefer my answer here I believe