Fun With Eclipse Debugging

I have a fun trick to share, but first I’d like to give a little background on why I needed it. The Eclipse debugger is great, however, Android doesn’t always let it see everything. Android likes to hide stuff from us programmers, and we can only see variables inside of a certain scope in the debugger. I was recently trying to debug a null error in this line:

resultListView.setAdapter(new ArrayAdapter(ResultView.this, android.R.layout.simple_list_item_1, task.getResult()));

and Eclipse was telling me that I had a null error at that line, but not giving me anything specific as to why. Furthermore, the debugger was able to see some pieces of data involved in this line, but not everything. Given the limitations in Android for seeing certain variables, I often will declare a dummy variable and stick whatever variable-from-another-scope I need inside of that to peek at it in the debugger. This is a nice trick that often helps, but I found another cool Eclipse ‘debugging hack’ that solved this issue really quickly for me.

I broke up the offending line of code into several lines such that it read:

(new ArrayAdapter


and, sure enough, Eclipse spit out a different line than the one it previously had given me. It told me specifically which piece of the original ‘line’ of code was the problem. (I’d like to note that I tested this with an intentionally bad null statement before I trusted it for debugging purposes…) So, the next time Eclipse tells you that one of your lines of code is junk, remember that you can avoid all that gross thinking stuff by mashing the enter key a few times and running it through the debugger again. What a brain-saver!