I noticed a strange error today when attempting to use the RegisterTaskDefinition() method from the TaskFolder class. If I use this overload:
```
RegisterTaskDefinition(string path, TaskDefinition definition, TaskCreation creationOption, string UserName)
```
Then I get a NullReferenceException. The portion of the stacktrace that shows where the problem is as follows:
```
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Microsoft.Win32.TaskScheduler
StackTrace:
at Microsoft.Win32.TaskScheduler.Task.GetV2Definition(TaskService svc, IRegisteredTask iTask, Boolean throwError)
at Microsoft.Win32.TaskScheduler.Task.CreateTask(TaskService svc, IRegisteredTask iTask, Boolean throwError)
at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String Path, TaskDefinition definition, TaskCreation createType, String UserId, String password, TaskLogonType LogonType, String sddl)
at MYAPPLIATION.GUI.SchedulingManager.addTaskButton_Click(Object sender, EventArgs e) in MYAPPLICATION\GUI\SchedulingManager.cs:line 175
```
If I remove the TaskCreation enum option and the username to use the overload that is only Name & Definition, then there are no issues. The option that I'm using is TaskCreation.ValidateOnly option since I don't want to actually register the task just yet.
Comments: ** Comment from web user: dahall **
```
RegisterTaskDefinition(string path, TaskDefinition definition, TaskCreation creationOption, string UserName)
```
Then I get a NullReferenceException. The portion of the stacktrace that shows where the problem is as follows:
```
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Microsoft.Win32.TaskScheduler
StackTrace:
at Microsoft.Win32.TaskScheduler.Task.GetV2Definition(TaskService svc, IRegisteredTask iTask, Boolean throwError)
at Microsoft.Win32.TaskScheduler.Task.CreateTask(TaskService svc, IRegisteredTask iTask, Boolean throwError)
at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String Path, TaskDefinition definition, TaskCreation createType, String UserId, String password, TaskLogonType LogonType, String sddl)
at MYAPPLIATION.GUI.SchedulingManager.addTaskButton_Click(Object sender, EventArgs e) in MYAPPLICATION\GUI\SchedulingManager.cs:line 175
```
If I remove the TaskCreation enum option and the username to use the overload that is only Name & Definition, then there are no issues. The option that I'm using is TaskCreation.ValidateOnly option since I don't want to actually register the task just yet.
Comments: ** Comment from web user: dahall **
If you are using the library, it is very, very hard to create an invalid TaskDefinition, so I'm not sure why you would need to validate anyway. There is a rich validation method (Validate) on the TaskDefinition class that will tell you exactly what your issues are.
Anyway, to answer your question, if there is a validation problem when registering using the ValidateOnly flag, it will throw an exception. If there is NOT a validation problem, then it will return 'null'. It cannot return a Task because one is not created.