Question

Okay, so I have a very large project in Java that I have been working on for years (a game, if you must know).

Unfortunately, a mysterious message has started being printed. No matter the lengths of my searching, I cannot pinpoint the location of the message.

I know that you can override the println method, and I was curious if it was possible to have some sort of debugging info attached to the message being printed? Like the line or file it was printed from perhaps? I tried searching Google for this, but it is simply too detailed of a problem that I cannot find an answer.

Even worse, the message is very ambiguous. It is simply "-1".

Atleast I know now to use a proper logger instead of simply println().

Was it helpful?

Solution

In such a case you can use a conditional breakpoint in your ide. This is a really useful feature of ide that's not always well known.

You set a breakpoint and you want it to suspend the application only under some condition, in this case the parameter of the println method being your 'mysterious ufindable string'

In the case of Intelllij IDEA this would look like this:

enter image description here

OTHER TIPS

You can do

 new Exception().printStackTrace()

or similar without throwing the created exception to get a full stack trace for a current execution point. And there is no need to throw it. You may event save them into list for later processing.

This looks like a message from ArrayIndexOutOfBoundsException. Add breakpoint on this type of exceptions. Try this:

String[] arr = new String[0];
try{
    System.out.println(arr[-1]);
}catch (Exception e){
    System.out.println(e.getMessage());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top