tags:

views:

35

answers:

2

I have an ASP.NET web app which is small yet has a fair amount of C# behind it.

I am trying to run some code, which is dependent on a class library/.dll I have produced (containing business logic). When I debug, after I bind to datasource property, I get an object reference not set to an instance object.

I know how to fix these errors as I have done more than my fair show when I lacked experience, but the trouble I have is I cannot find the last method to execute (which will in turn help me find the variable at fault) until this exception. Is there a way I can make the code break when it reaches the line of code causing this exception (or the line of code matching the last method called in the stacktrace)? I will probably do a find for that method signature but I don't really like this approach. Is this something for windbg?

I guess this is what they mean by unmaintainable code.

+1  A: 

Look at the stack trace that is most likely printed out with the error.

Also try breaking on exceptions - Debug Menu -> Exceptions, Choose CLR Exceptions

Luke Schafer
Ah yes, this was the option I was after. I read about this a few weeks ago too. I want the code to break and go back into VS and with the popup in the .cs file to blame.
dotnetdev
I'm glad that's what you were looking for :) Thanks for the accept - not enough people seem willing to do that!
Luke Schafer
A: 

VS should highlight the line from which the exception was thrown. If it doesn't, you can enable it in Debug > Exceptions. If you put a breakpoint on or before this line (or if it doesn't give you a line, on or before the code you suspect of throwing it), and then "Step In" repeatedly, you will eventually reach the code where the exception is thrown.

Fritz H
I forgot to write, after the line where I bind to the datasource, the exception occurs but there is no other line of code executed (well not hit while stepping through).
dotnetdev