0

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)
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • `e.printStackTrace()`? – Andremoniy Dec 01 '16 at 11:02
  • @Andremoniy And how do you redirect the output from `printStackTrace` from the default (error) console to your own logger? – Niklas P Dec 01 '16 at 11:04
  • MainWindow.logger.log(Level.SEVERE, InfoMessage.MSG_MUSICBRAINZ_QUERY_SYNTAX_ERROR.getMsg(song.getRecNo(), song.getFile().getName()),e); – Jens Dec 01 '16 at 11:05
  • 2
    Or use some logger and do `log.error(e.getMessage(), e)` – Andremoniy Dec 01 '16 at 11:05
  • not clear without the `try` part of the block. – avi.elkharrat Dec 01 '16 at 11:11
  • @Paul Taylor, you need to clarify the reason why you want to catch this exception in the first place, and why the reference to the exact place where it was raised is so important. On a functional bassis, it doesn't seem so important: it seems like you have a NPE, a.k.a. a bug that you should fix. After that, would you still give one to this exception? – avi.elkharrat Dec 01 '16 at 11:15
  • 1
    @avi613 the reference is important because that shows me what the problem is, without it I dont what code is caysing the exception, only that there was a NullPointerException. Im using the standard Java logging framework – Paul Taylor Dec 01 '16 at 11:17
  • @Paul, please see my comment, just above :) – avi.elkharrat Dec 01 '16 at 11:18
  • 1
    @PaulTaylor okay few things that might help here is the logger implementation that you are using (`MainWindow`) and the line of code throwing the exception. – Naman Dec 01 '16 at 11:19
  • Are you positive there is no difference between e and e.getCause()? Because it really does look like that's the issue, but it's really hard to say if something else more subtle isn't going on, can you include some context? What is line 37 of MusicBrainzSoundtrackChecker.java? What is stats? Can it be null? – Jason C Dec 01 '16 at 11:22
  • @nullpointer hmm maybe it is my logger, since e.printStackTrace() does indeed ouput okay, Ive updated my question – Paul Taylor Dec 01 '16 at 11:23
  • @JasonC the failing code really is not the issue, the issue is the exception handling – Paul Taylor Dec 01 '16 at 11:24
  • @Paul Did you write your logger yourself? If so can you post the relevant part of its implementation? – Jason C Dec 01 '16 at 11:26
  • @PaulTaylor cool, sorted here :) – Naman Dec 01 '16 at 11:26
  • Ah found it, sorry. Its not the logger but the call that logger make to get an error message InfoMessage.MSG_MUSICBRAINZ_QUERY_SYNTAX_ERROR.getMsg(song.getRecNo(), song.getFile().getName()) that is causing the issue. Thanks for your help everybody – Paul Taylor Dec 01 '16 at 11:28
  • I think that you should catch the exception in the caller of this method, making this method throwing the RuntimeException – Igino Boffa Dec 01 '16 at 11:05

1 Answers1

0

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.

avi.elkharrat
  • 6,100
  • 6
  • 41
  • 47
  • Yes that was the issue, the problem was that song variable itself was null, I hant noticed because I was using another song variable in local scope that wasnt visible to the catch block – Paul Taylor Dec 01 '16 at 11:56
  • Regarding npes, my code uses some webservices api that sometimes change or break and i don't not have control over these, so sometimes new problems occur that I couldn't have easily tested for before. Also where I catch such exceptions my app can just log and continue, letting the exception propagate up to end user would be meaningless and not helpful to the user. – Paul Taylor Dec 03 '16 at 10:20