7

I have a c# Azure project that is just a web api, and wanted to remove some of the non-API default components (welcome page, help, etc). After doing so, when launching the application, I get an error message "Object reference not set to an instance of an object." I've seen this before when doing things in Azure, and somehow got around them each time. My question really is: how can I debug this error on my own? This error description is woefully lacking in detail (there's no call stack or anything, as if the app hasn't yet launched, and the dialog is modal so I can't click on anything within VS)

Extra notes:

  • it's an ASP .NET project.
  • it works once I publish to Azure.
  • after copying the project entirely and rebuilding, it seemed to work. then when mucking with NuGet dependencies, we're back to it not working (i've tried deleting my packages folder and having NuGet recreate - no luck; no idea if related to NuGet at all)
  • I re-copied the project, so just 'MySolutionFolder' -> 'MySolutionFolder - Copy', reopened with same version of VS, runs fine. This is an amazingly sloppy solution obviously, so looking for something better.

As described in a comment, the issue presumably dealt with access rights to various files, most likely NuGet related but possibly Azure as well, which explains why copying to a new solution directory 'fixed' the issue. I would love to see better error info, such as "Error: Visual Studio couldn't write to 'packages.config'", so I know actually where to fix this sort of issue. As it stands now, it's like a compilation error that says "Syntax error in code. Somewhere. Good luck finding it".

Rollie
  • 4,391
  • 3
  • 33
  • 55
  • Can you post something so we can see what is happening ? – deathismyfriend Feb 14 '14 at 07:52
  • It's really nothing more than what I described - an error box with the aforementioned text, then nothing. It's exactly like the debug button is linked directly to code to 'Modal.Show("error"); exit" - nothing to signify the app attempted to start at all – Rollie Feb 14 '14 at 08:00
  • Almost sounds like VS error, not your app. Maybe take a look to the Windows Event Log... – Michal Levý Feb 24 '14 at 18:50

11 Answers11

11

Unfortunately, I never did discover a way to debug issues of this sort in a more general way. I eventually worked around the problem, and then saw it pop up again; following advice someone else tried on another forum, I deleted my packages folder, and had NuGet recreate it, which for whatever reason corrected the issue.

Another time, a solution from this question worked (remove and re-add all roles); sometimes this seemed to require an Azure SDK reinstall and computer restart as well.

EDIT

Finally, the best solution I could find was here: delete SUO files and restart VS.

Hopefully this solution will help someone with a similar problem in the future!

Community
  • 1
  • 1
Rollie
  • 4,391
  • 3
  • 33
  • 55
7

tick Debug > Exceptions > Common Language Runtime Exceptions > System > NullReferenceException and run your app

user626528
  • 13,999
  • 30
  • 78
  • 146
  • Gave it a shot - that one was already selected (user handled box). I tried 'Thrown' as well, no dice. Tried selecting all 'Thrown' boxes, and nada. – Rollie Feb 14 '14 at 07:58
  • @Rollie, are you sure your debugger is attached with a correct debugging engine? Check "Debug/Attach to Process" dialog. – user626528 Feb 23 '14 at 03:52
  • I'm actually running from Debug in VS, and as described this debugging process works fine after the solution is copied to a new directory. I likely found the issue (access rights), but the question here isn't "how do I make it run", it's "how do I debug this myself so I don't have to ask again". The answer I'm going for is "oh if you run VisualStudioInternalErrorViewer.exe in the install directory, you can see errors that occur with VS itself in more detail", or something of that sort – Rollie Feb 24 '14 at 02:10
2

You can enable Intellitrace and remote debug your azure services:

http://blogs.msdn.com/b/webdev/archive/2013/11/05/remote-debugging-a-window-azure-web-site-with-visual-studio-2013.aspx

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
  • In this case, I'm more interested in what I can do to diagnose and fix the issue locally, than work around it by debugging in a different environment. – Rollie Feb 17 '14 at 01:40
2

I've seen similar symptoms when something goes wrong very early in the asp.net app start-up cycle (before the debugger has attached).

One workaround I have used before:

  1. Make sure you are debugging with a local IIS instance and not IIS Express (right click project properties -> Web -> Servers dropdown -> Local IIS
  2. Hit F5 and get your error dialog. (Just to make sure the IIS process has started). Close the error dialog.
  3. Debug -> Attach to Process -> attach to w3wp.exe (iis process). If it's not there make sure 'show processes from all users/sessions' are both checked.
  4. Force a restart of your app by changing the web.config file.

The restart will now occur with the debugger attached to the process from the very start. If that doesn't give any more useful info, then look at turning off 'Just My Code' debugging exception/breakpoint handling and repeating.

Tyson
  • 14,726
  • 6
  • 31
  • 43
  • For #1, I don't see that option anywhere in either the ASP.NET project or the Azure project (both do have a web tab, albeit with different options). For #3, the error box in this case is modal, and does not allow use of the various menus, including Debug, so I can't attach to the IIS process unless I do it with another instance of VS - is this your suggestion? I will try to set up the rest of this if I have some time later, as currently I worked around the issue - very interesting approach though, curious to see if it yields results. – Rollie Feb 24 '14 at 02:49
  • @Rollie I edited my answer to account for VS2013 changes, and also to explain that #2 step a bit better. However if the error you are getting is a modal VS dialog, it sounds like it might not be anything to do with the asp.net app start at all, and something going wrong within VS instead. So my above steps might not help in this case at all. As you mentioned (but not what I was getting at), another option is to attach another instance of VS to your current one, but its a bit of a reach. – Tyson Feb 24 '14 at 03:15
1

For Visual Studio 2019, click on menu "Debug > Windows > Exception Settings".

Under Common Language Runtime Exceptions, tick on System.NullReferenceException. Debugger will break at the point where this exception is caught.

enter image description here

TPG
  • 2,811
  • 1
  • 31
  • 52
0

You can setup a local Azure emulator, this is how I've always debugged my cloud apps.

http://msdn.microsoft.com/en-us/library/windowsazure/hh403990.aspx

And maybe do some reading! http://msdn.microsoft.com/en-us/library/windowsazure/ff683670.aspx http://msdn.microsoft.com/en-us/library/windowsazure/hh694035.aspx

matt_lethargic
  • 2,706
  • 1
  • 18
  • 33
  • I have done this, and had it working in various states - the problem I have been having (and making progress on) is related to VS throwing errors with no obvious avenue to figure out *why* the errors are being generated. – Rollie Feb 20 '14 at 01:25
  • Event Viewer? Stack Trace? – matt_lethargic Feb 21 '14 at 10:46
  • There is no stack as the application doesn't actually run. Nothing meaningful in event viewer - thanks for the idea tho! – Rollie Feb 24 '14 at 02:06
0

Inside global.asax:

protected void Application_Error(object sender, EventArgs e)
{
    var error = Server.GetLastError();
    string message = error.Message;
    string callStack = error.StackTrace();  
    //Write message and callStack to file
    ...
}
acarlon
  • 16,764
  • 7
  • 75
  • 94
0

I don't think debugging will help in this case.

However, unloading/reloading your Azure project by right-clicking on it might fix. I'd tried a LOT of things before discovering this one.

As mentioned here.

Community
  • 1
  • 1
Dunc
  • 18,404
  • 6
  • 86
  • 103
  • Yeah that worked sometimes, but the real 100% solution was to close the solution and delete the SUO file (see my own accepted answer to this question) – Rollie Jun 26 '14 at 09:17
0

I also had the "Object reference not set" issue starting Compute Emulator. I found running VS as administrator resolved this.

AntonK
  • 2,303
  • 1
  • 20
  • 26
0

Changing project target to 4.6.1. seems to have worked for me.

Tomas
  • 61
  • 5
-1

You can debug through logs. Like you can check through diagnostic logs. Enable them through code. There are some configuration for it. I also faced this type of issue and that was very difficult for me to trace. So i enabled the diagnostic logs and write on logs on my custom check point.