I got this error when I use Visual Studio 2010 to program some SharePoint 2010 stuff. It is a simple console program. This exception happens at the following line:
SPWebApplication webApplication = SPWebApplication.Lookup(new Uri("http://mysiteurl"));
Below is another error:
The Web application at http://mysiteurl could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.
and this happens at line
SPSite site = new SPSite("http://mysiteurl");
Both of the errors have the same root cause.
Right Click your project, Choose Properties. Go to tab “Build”, Change Platform Target from “x86” to “Any CPU”. Recompile your project and you will be all set to go.