This project is read-only.

Object reference not set to an instance of an object

Aug 24, 2009 at 8:57 PM

Hi,

I'm having a little trouble figuring out how to make your example working on my MOSS 2007 server (Windows Server 2003 and Visual Studio 2005).

This is the error message:

Object reference not set to an instance of an object.   at TPG.SharePoint.Workflow.TaskData.GetProperty(Object key)
   at TPG.SharePoint.Workflow.TaskData.get_Item(Object key)
   at ASP._controltemplates_workflowdemo_approvaltaskcontrol_ascx.LoadData(Boolean isInitialLoad) in c:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES\WorkflowDemo\ApprovalTaskControl.ascx:line 39
   at TPG.SharePoint.Workflow.WorkflowDataControl`2.OnLoad(EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

This is ApprovalTaskcontrol.ascx:

<script type="text/C#" runat="server">
    protected override void LoadData(bool isInitialLoad)
    {
        if (isInitialLoad)
        {
            TaskData taskData = GetData();
                    CommentsField.Text = taskData["MyComments"] as string;
        }
    }

    protected override TaskData SaveData(object commandArgument)
    {
        TaskData taskData = new TaskData();
        taskData["MyComments"] = CommentsField.Text;
        taskData["MyApprovalStatus"] = commandArgument;

        return taskData;
    }   
</script>

This is workflow.xml:

<Workflow
       Name="WorkflowDemo"
       Description="Description for WorkflowDemo"
       Id="59e4c307-a8e0-49cd-904a-60b67f27c61e"
       CodeBesideClass="WorkflowDemo.WorkflowDemo"
       CodeBesideAssembly="WorkflowDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7ab4fcd1757060a0"
       TaskListContentTypeId="0x010801004E4FD5D508784092AA20394584759B99"
       AssociationUrl="_layouts/WSS3Workflow/AssociationFormHost.aspx"
       InstantiationUrl="_layouts/WSS3Workflow/InstantiationFormHost.aspx"
       StatusUrl="_layouts/WrkStat.aspx">
    <MetaData>
        <Association_FormUrl>/_controltemplates/WorkflowDemo/WorkflowConfiguration.ascx</Association_FormUrl>
        <Instantiation_FormUrl>/_controltemplates/WorkflowDemo/WorkflowConfiguration.ascx</Instantiation_FormUrl>
        <Task_0_FormUrl>/_controltemplates/WorkflowDemo/ApprovalTaskControl.ascx</Task_0_FormUrl>
    </MetaData>
  </Workflow>

 

If I comment out CommentsField.Text = taskData["MyComments"] as string; it works fine. I followed just as you presented in your video. Can you help?

Thank you!

Keio

Aug 31, 2009 at 2:53 PM

Hi Keio!

To solve the problem, try following workaround:

take your code after isInitialLoad into a try-catch block like this:

<script type="text/C#" runat="server">
    protected override void LoadData(bool isInitialLoad)
    {
        if (isInitialLoad)
        {

              try{

                      TaskData taskData = GetData();
                             CommentsField.Text = taskData["MyComments"] as string;
             }catch{}

        }
    }

It should fix the problem. The LoadData method is used to retrieve saved data when you are clicking on edit after finishing the task. If your LoadData is empty, you won't see your already given data on the AssociationForm. But if you use it in a try-catch block, the first time when you open the form, it will throw an nullreferenceexception, because the taskData instance doesn't contain any data, because you haven't given any yet.

Try it out!

Good Luck!

Karesz

Aug 31, 2009 at 2:56 PM

... and after the second time you open the form you'll see all the information you've given before, because the taskData already contains those data, beacuse it's been saved after clicking the commit button.

Sorry, i forgot to finish my last sentence!

Aug 31, 2009 at 4:28 PM

Karesz,

It works like a charm.

Thank you!

Nov 23, 2009 at 11:10 AM

I was wondering the same.  The video didn't have a try/catch did it?  It seems excessive to throw a nullrefereenceexception for an item not existing in an index.  Returning a null might have been more appropriate?

Craig

Jul 2, 2010 at 2:56 PM

I agree that cathcing the exception is not the best soluition, but the TaskData doesn't have anyway to iterate the keys to search "MyComments"... so i don't know if there is another approach to solve this.

Another approach that I think can work, is add the field MyComments to the task list, because i believe the taskdata will load this field....

What do you think?

 

 

Jul 2, 2010 at 2:59 PM

I don't do SharePoint development anymore and don't remember enough about this to comment, sorry :)