Reputation: 8610
I have a peace of code that is poorly written, like so:
HashMap<String, TceFile> mapPresetLastKdf = new HashMap<String, TceFile>( (HashMap<String, TceFile>) RsessionVars.get( sessionVars, Constants.SVAR_ENC_PRESET_LAST_KDF_MAP ) );
HashMap<String, String> mapPresetApid = new HashMap<String, String >( (HashMap<String, String >) RsessionVars.get( sessionVars, Constants.SVAR_ENC_PRESET_APID_MAP ) );
Clearly, first line fails if RsessionVars.get
returns null
. In that case second line is never executed.
However, Eclips IDE does not signal this failure to me, it continues executing as all is OK.
I am affraid I have many places in the code like this one. How can I detect those?
How can I force Eclipse to stop and report error on lines like this?
Edit:
This seems to be problem with exception handling.
HashMap<String, TceFile> mymap = new HashMap<String, TceFile>( null );
This should report null
pointer exception.
Why is this exception not visible anywhere?
Upvotes: 0
Views: 423
Reputation: 16390
There's a difference between an exception, which you can catch, and the logical cause of the exception, which you can not catch at all. In this case, the returning and subsequent setting of the null value is a valid thing to do. It's likely in a subsequent statement that you try to send this variable (and therefore a null) some method and you're getting a null pointer exception.
In cases like this, you can only break on the subsequent null pointer exception, realize that that the cause is a null value in your variable and then to go back and debug again using breakpoints in the code to see where that null value came from.
Upvotes: 1
Reputation: 1
Maybe your workspace is not setted correctly to handle such errors. If you go under Window -> Preferences -> Java -> Debug you should have a list of options available. The one you might want to look into is the "Suspend execution on uncaught exceptions". If checked, Eclipse will put an automatic breakpoint if the exception is not caught.
Also, if you ever know what type of exception might be, you can add a "Java Exception Breakpoint", available in the Debug/Breakpoint window (the "J" with and exclamation mark), it's an useful feature when you know what you are searching. Other than that, seperating your code in multiple lines is easier to read and to debug.
Upvotes: 0
Reputation: 1351
You could go around eclipse and write something that notifies you of the error before creating the HashMaps:
if (RsessionVars.get(sessionVars, Constants.SVAR_ENC_PRESET_LAST_KDF_MAP) == null)
System.out.println("RsessionVars.get() returned null");
Upvotes: 0
Reputation: 6206
You probably want to break the code up into multiple lines and either use a conditional breakpoint (which you can do by right clicking the line numbers if you have them displayed) or use an if statement with a normal breakpoint.
I've had trouble with conditional breakpoints and eclipse in the past so I usually go with the latter.
Upvotes: 0