2

Ok so I totally new to C# and am trying to debug an error. Basically I am trying to create an EventReceiver for a SharePoint List...this is the code that is giving me the object reference error when I am debugging:

   public override void ItemAdding(SPItemEventProperties properties)
   {
       base.ItemAdding(properties);

       SPListItem item = properties.ListItem;

       if (item["Name"] == null)
           return; //or better yet, log 

       string oldFileName = item["Name"].ToString();

What I am doing is entering Debug mode, and selecting to add a file to a SharePoint library (this is in ItemAdding event), now this error is shown after I select the file I want to upload, any idea why?

Thanks for any help!

2
  • 1
    You should tag this question with sharepoint Sep 6, 2012 at 20:57
  • Are you sure that properties.ListItem is not null? Sep 6, 2012 at 20:59

3 Answers 3

4

It's not an "object reference error", it's a NullReferenceException caused by the fact that you are trying access the index operator of item, which is null.

You could have found this out by setting a breakpoint in the line of the if statement and hovering your mouse over the different variables.

To fix this, make sure properties.ListItem contains a non-null value or insert another check in your if:

if (item == null || item["Name"] == null)
5
  • Thanks! That seems to work, I tried using breakpoints, however, as soon as I enter debug mode my breakpoint turns yellow and hovering over it I get the message "Breakpoint will not be hit. No symbols have been loaded for this document" any idea why this is? Sep 6, 2012 at 21:08
  • @TudorHofnar try to keep it running eventually the symbols will be loaded and the breakpoint will be hit.
    – Stan R.
    Sep 6, 2012 at 21:16
  • There's a relevant SO question that offers a few possible solutions.
    – Adam
    Sep 6, 2012 at 21:16
  • @StanR. I left it running for about 10 min and the break points still haven't loaded (while I was replying to emails haha) Sep 6, 2012 at 21:51
  • @codesparkle I had a read through that thread and tried a few of the option but nothin worked. Right click on solution --> Properties Look under Common Properties --> Startup Project Select multiple startup projects select Start action on the projects you need to debug. That solution in particular I can't perform since I can't find Common Properties, I am doing all this in an Even Receiver project. Any idea how I can try that out? Sep 6, 2012 at 21:52
0

You probably got the error because SPListItem item is null. You cannot acces to null variable. You may try to update your code to:

       SPListItem item = properties.ListItem;

       if (item == null || item["Name"] == null) 
           return; //or better yet, log
0
0
SPListItem item = properties.ListItem;
System.Debug.Assert(item != null, "item is null.");


if (item["Name"] == null) --DEBUGGER STOPS HERE
    return; //or better yet, log 

it seems that item or more specific properties.ListItem is null! As item is just a reference.

1
  • properties.ListItem and item are both references.
    – Adam
    Sep 6, 2012 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.