Thursday, 21 August 2008

FindControl Causes Null Reference Exception with NMock

When using NMock on code that uses FindControl, a null reference exception is thrown:

PlaceHolder placeHolder =

(PlaceHolder) _view.MyPanel.FindControl("myplaceholder");

You could use the indexer instead:

PlaceHolder placeHolder =

(PlaceHolder) _view.MyPanel.Controls[0];

But if you insert another control in the panel, you break your code. So, the alternative is to loop through the controls and find a match by ID:

ControlCollection ctrls = _view.MyPanel.Controls;

foreach( Control ctrl in ctrls )
if( ctrl.ID == "myplaceholder" )
placeHolder = (PlaceHolder) ctrl;

Saturday, 2 August 2008

CruiseControl builds not working

Here's a quick checklist to assist in getting failed builds working again in CruiseControl.NET:
  • Comment out any custom tasks, such as running unit tests, in the ccnet config file (defined within <exec> elements)
  • Find error details quickly within the build log by searching for the following words/phrases:
    • could not
    • failed
    • success="false"
  • Check whether the code compiled without error by:
    • Running the project's team build as used by CCNET
    • Searching for "csc" (or "vbc") in the build log
  • Clear out temporary files on the build server to remove any sticky references