8

I got following error...

System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.SharePoint.SPListItem.get_UniqueId() at ConsoleApplication1.Program.Main(String[] args) in Program.cs:line 21

running following code

using (SPSite site = new SPSite("http://site/"))
{    
    using (SPWeb web = site.OpenWeb())
    {
        try
        {
            SPList list = web.Lists["ListName"]; // 2        
            SPListItem item = list.Items.Add();
            Guid itemId = item.UniqueId;
            SPListItem itemUpdate = web.Lists["ListName"].Items[itemId];
            itemUpdate["PercentComplete"] = .45; // 45%        
            itemUpdate.Update();
        }
        catch (Exception e)
        { 
            Console.WriteLine(e);
            Console.ReadLine();

        }
    }
}

What's the problem?

AnonJr
  • 2,759
  • 1
  • 26
  • 39
K-M
  • 660
  • 3
  • 13
  • 27

4 Answers4

11

If you're trying to alter values for a just inserted list item, you should go with:

SPList list = web.Lists["ListName"];
//SPListItem item = list.Items.Add();
//item["PercentComplete"] = .45; // 45%
//item.Update();

SPListItemCollection items = list.GetItems(new SPQuery()
{
    Query = @"<Where>
                <Eq>
                   <FieldRef Name='Title' />
                   <Value Type='Text'>Desigining</Value>
                </Eq>
              </Where>"
});

foreach (SPListItem item in items)
{
    item["PercentComplete"] = .45; // 45%
    item.Update();
}

You just need to use list.Items[uniqueId] or faster list.GetItemByUniqueId(uniqueId) if you needs to find a particular item to update; what can be accomplished by using SPQuery class.

Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • THANK YOU SO SO MUCH! thank you! will try it now and let u know. really appreciate it – K-M Oct 16 '09 at 15:26
  • 2
    i had few errors but i managed to fix them by looking at the SPQuery class for MS website. Once again thank you for all your help! really appreciate it! – K-M Oct 16 '09 at 15:46
4

Ruben's answer was correct but was getting few errors (may be its was only for me) therefore i tweaked little bit and then it was working fine. Below is the code which i used if anyone needs it

 SPList list = web.Lists["ListName"];
                    //SPListItem item = list.Items.Add();
                    //item["PercentComplete"] = .45; 
                    // 45%//item.Update();
                   SPQuery oQuery = new SPQuery();

                        oQuery.Query = @"<Where>               
                                    <Eq>                   
                                        <FieldRef Name='Title' />                   
                                        <Value Type='Text'>Design</Value>              
                                    </Eq>            
                                  </Where>";
                        SPListItemCollection collListItems = list.GetItems(oQuery);
                        foreach (SPListItem item in collListItems)
                    {    item["PercentComplete"] = .55;   
                        item.Update();}
jpaugh
  • 6,634
  • 4
  • 38
  • 90
K-M
  • 660
  • 3
  • 13
  • 27
1

Try calling Update () on the list before getting the UniqueID

 SPList list = web.Lists["ListName"]; // 2        
 SPListItem item = list.Items.Add();
 item["Title"] = "Test";
 item.Update ();
 list.Update ();
 Guid itemId = item.UniqueId;
axel_c
  • 6,557
  • 2
  • 28
  • 41
  • Thanks! for changing the name. its giving me the same error System.NullReferenceException: Object reference not set to an instance of an obj ect. at Microsoft.SharePoint.SPListItem.get_UniqueId() at ConsoleApplication1.Program.Main(String[] args) in C:\Inetpub\wwwroot\......\Program.cs:line 22 – K-M Oct 16 '09 at 14:36
  • Modified example to set a dummy property and call Update () on item before fetching Id, don't have a MOSS here to test, but that should work. – axel_c Oct 16 '09 at 14:43
  • works but creates a new fresh entry rather than updating it. what i actually wanted was updating the exisitng entry – K-M Oct 16 '09 at 14:51
0

My best quess is that your item is not yet created in the list when you do:

Guid itemId = item.UniqueId;
SPListItem itemUpdate = web.Lists["ListName"].Items[itemId];

First do a item.Update() before requesting the uniqueId and/or getting the item back from a list.

PS : I see no reason why you should get a second SPItem object for updating the 'PercentComplete' information.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Wout
  • 964
  • 1
  • 6
  • 19
  • thank you :) - any ideas about http://stackoverflow.com/questions/1588019/programmatically-insert-a-list-as-a-webpart-in-a-webpart-page-in-wss-3-0 please? – K-M Oct 19 '09 at 10:55