Paul Taylor Paul Taylor - 8 days ago 6
Java Question

How do I see the full stack trace when I catch an exception

My Java code has this (but leaving aside whether I should catch RuntimeException I have my reasons)

catch(RuntimeException e) {
MainWindow.logger.log(Level.SEVERE, InfoMessage.MSG_MUSICBRAINZ_QUERY_SYNTAX_ERROR.getMsg(song.getRecNo(), song.getFile().getName()),e.getCause());
stats.incUnableToMatchIds();
}


I don't understand why when an exception does occur it only goes as far as this catch block, so it doesn't pinpoint where the exception occurs. There is no difference whether I use e.getCause() or e

java.lang.NullPointerException
at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:234)
at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:42)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)


if I remove this catch block it then shows me the full stacktrace which is what I want it to do,

java.lang.NullPointerException
at com.jthink.jaikoz.manipulate.classical.MusicBrainzSoundtrackChecker.updateSoundtrack(MusicBrainzSoundtrackChecker.java:37)
at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:152)
at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:42)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)





How do I get the com.jthink.jaikoz.manipulate.classical.MusicBrainzSoundtrackChecker.updateSoundtrack(MusicBrainzSoundtrackChecker.java:37) line part using the logger in catch?

Edit

Perhaps as suggested it is my logger, since if I add in

catch(RuntimeException e)
{
e.printStackTrace();
MainWindow.logger.log(Level.SEVERE,
}

I get the output ok with e.printStackTrace()

java.lang.NullPointerException
at com.jthink.jaikoz.manipulate.classical.MusicBrainzSoundtrackChecker.updateSoundtrack(MusicBrainzSoundtrackChecker.java:37)
at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:152)
at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:42)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
01/12/2016 11.18.53:com.jthink.jaikoz.manipulate.Analyser:waitForWorkers:WARNING: Worker:0:Exception:null
java.lang.NullPointerException
at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:235)
at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:42)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

Answer

So you have an NPE. In other words, you're calling a method on an object that has not been initialized (i.e. constructed).
Sometimes, it can be tricky to solve, because the non-initialized object can be nested in a nested in a nested... object and the method call generating this exception can be very remote.

Here is a hypothesis: if you don't have the same stacktrace without the try / catch block, than maybe it's a different exception. What i mean is: maybe your catch block itself is generating an NPE from a different location than the one in the try block. This is suggested by your logging line:

InfoMessage.MSG_MUSICBRAINZ_QUERY_SYNTAX_ERROR.getMsg(song.getRecNo(), song.getFile().getName()),e.getCause());

There is too much going on there! Try debug this code, with and without try / catch, inspecting every object. I'm sure, you'll find a different NPE in each case.

For more background, NPE should not happen in production code. You should pay attention that all your objects (and sub-objects) are initialized.
Many recently developped languages forbid or find ways to prevent this error.
Please see this reference: Tony Hoare, regretting the invention of null.