“Uncompilable source code” exception in NetBeans
If your project builds fine but then crashes with a runtime exception when it hits a block of code, the problem is probably with the “Compile on save” option. Right click on your project, choose Properties, Build -> Compiling. Uncheck Compile on Save. Manually build the project. When you re-run the code, the errors should go away.
Not sure what causes this, but it’s immensely frustrating and seemingly non-deterministic. Wish I had a better solution than this, but I don’t.
I found perhaps a better solution on StackOverflow:
We finally got a solution, but still don’t quite know why the situation occurs. When you have Compile On Save activated, Netbeans generates a second set of class files for debugging etc. These are stored in $USER/.netbeans/var/cache/index/s*/java/*/classes
Somehow (not sure how) this directory can get corrupted or fail to update.
If you close netbeans, delete $USER/.netbeans/var/cache/index and all subdirectories and restart netbeans this clears the cache. If you have no compile errors, your problem ought to go away at this point.
NB: $USER is your user directory – on Windows 7 this is usually c:\Users\username, I guess on Unix it will be ~username.
If you get this problem please vote for, comment on, or add information to: http://netbeans.org/bugzilla/show_bug.cgi?id=182009
Thanks to Nick Fortescue for this information.