可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I could use some help trying to track down an intermittent error that I've been having with our ASP.Net project for quite some time.
Intermittently when building the solution, the build will fail with the error "/: Build (web): Object reference not set to an instance of an object." The error has no associated file, line, column or project information. The weird thing about the error is that it will go away on successive rebuilds and doesn't seem to result in any run-time errors that we've come across once the build is successful. Sometimes the error will pop only once, sometimes 3-4 times, but eventually the build will finish successfully and then seems to build just fine each time after. I haven't been able to nail down a pattern as to why and when the error will happen, and since it always eventually builds it hasn't been a critical problem for us. Just an annoyance. But one that I want gone for obvious reasons.
I guess I should add that this is an application that was originally developed in ASP.net 1.1 and converted to 2.0 and I inherited it somewhere down the line after that, so I don't know when the problem originally surfaced. As far as everyone here is concerned, it's always been there.
Obviously I'm not expecting someone to pick out the cause of my problem as that would require them to look at our entire solution to pick out potential problems. Just hoping someone can give me a couple fresh ideas as to how to go about tracking down the actual source of the error in code. It has to be coming from somewhere, right? How would you go about finding out where?
回答1:
I've seen this when you have a web control in a page where there is invalid HTML. If your codebehind is trying to do something with the control, it won't be able to find it and will give you Object Reference... error at compile time. In my experience, it doesn't create a runtime error, and the project will build if the file in question is closed at the time of build. HTH, Good Luck!
回答2:
Run this command at the command line and see if you get some more detailed information
%WINDIR%\Microsoft.NET\Framework\v3.5\msbuild.exe YourSolution.sln /v:n
回答3:
To follow up on this problem, we never did track down the origin of the error but it disappeared when we upgraded to Visual Studio 2008 and converted the project to a Web Application.
回答4:
I had this problem for a long time and finally found a solution that work fine for me.
It doesn't make sense to me... but altering my web.config file with the following gets definitively rid of this intermittent build error :
<buildProviders>
<add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<!-- add this line below -->
<remove extension=".rdlc"/>
</buildProviders>
Hope this help !
回答5:
I had this at build time when my project contained custom datasources (my own objects returning collections) with compile errors (that is, my objects had errors).
You'll also get this error if you try and add a datasource and your project doesn't have any datasources in the project's root (e.g. if you've put all your datasource classes in a subfolder). The only solution I found was to create a datasource in the project's root.
Sorry not to be more precise, but there seems to be several things that can go wrong with datasources/objects at compile-time.
回答6:
The first thing I'd try would be to increase the compiler verbosity. This can be set in the Visual Studio options - e.g. "Tools->Options->Projects and Solutions-Build and Run->MSBuild project build output verbosity" for VS2005. If you set it to diagnostic then it should tell you what it's doing at the time the exception is raised at the very least.
回答7:
An "Object reference not set to an instance of an object" is clearly a run-time error, not a compile-time error. So what that says to me is that Visual Studio is choking on something, which may not necessarily be in your code, or which something in your code is only indirectly causing.
Next question I'd ask: Does this happen only in Visual Studio, or does the same thing show up when you build using MSBuild or CSC?
回答8:
What's really odd is that it's a run-time error. You shouldn't see that at compile time. Do you have any pre- or post- build steps attached to the solution? Any unit tests you're including with your 'build' process?
Where does this error show up?
回答9:
Check the Application Log of your Event Viewer - It should tell you where the exception is being thrown.
回答10:
Just to clarify, is it the compiler itself that is choking? Are you doing anything weird with #define and #if directives in your code? Maybe something is being done out of order at some point... Just a thought...
回答11:
See if there are any post-build events that could be failing. These can be found on each project's property page.
Try using Rebuild Solution instead of Build Solution. You may need to add Rebuild Solution from Tools > Customize. If your web app installs or registers any windows services, and those services are started, Rebuilding plows through those types of problems.