SPFarm.Local is null in SharePoint 2010 causing C# Console App to throw a “Object reference not set to an instance of an object” error

Written by Cornelius J. van Dyk on . Posted in Blog

This one is one of the more annoying errors you’ll encounter in SharePoint development. The simple reason for that is because the error you’re receiving is bogus. I mean, sure it’s a “valid” error since the SPFarm.Local is actually null and you’re trying to reference it, but it doesn’t actually lead you anywhere. As a SharePoint developer/admin/architect, you will often be faced with the need to iterate content in the farm. Every good SharePoint developer knows that it all starts with the simple statement:

SPWebService svc = SPFarm.Local.Services.GetValue<SPWebService>(“”);
This should NEVER fail, unless you don’t actually have a SharePoint farm on your DEV box, but naturally you do, right? 🙂 So then why would you ever see the below from your debugger window? image[18] The problem stems from the architectural nature of SharePoint. SharePoint as you know, has been purely x64 based since the 2010 version. Add to that the fact that most utilities we write in the C# world to pull information from SharePoint, are Console apps, and you have the ingredients for our problem which is caused by Windows attempting to run a x86 based app under a x64 based architecture. Now where this should normally work, it doesn’t in this case. The real problem is that no architectural error is thrown. The code simply returns null and does not work. It can lead to many frustrating hours of debugging. Since console apps are by their very nature x86 based, it’s very easy to make the mistake of compiling your console app to the x86 architecture. If we go to the project properties for this console app, we’d find the following: image The “Platform target” is the setting we’re interested in. By default Visual Studio 2010 will set this value to x86 for console apps. It’s easy to forget to reset this value when quickly knocking out a console app. Here’s how this should be set: image Once we make this simple change and press F5, we’ll be able to step past the problematic line of code and continue on our merry way. Hopefully this article will save someone some time and frustration.



Trackback from your site.

Cornelius J. van Dyk

Born and raised in South Africa during the 70's I got my start in computers when a game on my Sinclair ZX Spectrum crashed, revealing it's BASIC source code. The ZX had a whopping 48K of memory which was considered to be a lot in the Commodore Vic20 era, but more importantly, it had BASIC built into the soft touch keyboard. Teaching myself to program, I coded my first commercial program at age 15.

After graduating high school at 17, I joined the South African Air Force, graduating the Academy and becoming a Pilot with the rank of First Lieutenant by age 20. After serving my country for six years, I made my way back into computer software.

Continuing my education, I graduated Suma Cum Laude from the Computer Training Institute before joining First National Bank where my work won the Smithsonian Award for Technological Innovation in the field of Banking and Insurance. Soon I met Will Coleman from Amdahl SA, who introduced me to a little known programming language named Huron/ObjectStar. As fate would have it, this unknown language and Y2K brought me to the USA in 1998.

I got involved with SharePoint after playing around with the Beta for SharePoint Portal Server 2003. Leaving my career at Rexnord to become a consultant in 2004, I was first awarded the Microsoft Most Valuable Professional Award for SharePoint in 2005, becoming only the 9th MVP for WSS at the time. I fulfilled a life long dream by pledging allegiance to the Flag as a US citizen in 2006. I met the love of my life and became a private consultant in 2008. I was honored to receive my ninth MVP award for SharePoint Server in 2013.

Leave a comment

You must be logged in to post a comment.