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!

Tudor Hofnar
  • 267
  • 2
  • 9
  • 20

3 Answers3

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)
Adam
  • 15,537
  • 2
  • 42
  • 63
  • 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? – Tudor Hofnar Sep 06 '12 at 21:08
  • @TudorHofnar try to keep it running eventually the symbols will be loaded and the breakpoint will be hit. – Stan R. Sep 06 '12 at 21:16
  • There's a [relevant SO question](http://stackoverflow.com/questions/2301216/the-breakpoint-will-not-currently-be-hit-no-symbols-have-been-loaded-for-this-d) that offers a few possible solutions. – Adam Sep 06 '12 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) – Tudor Hofnar Sep 06 '12 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? – Tudor Hofnar Sep 06 '12 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
petro.sidlovskyy
  • 5,075
  • 1
  • 25
  • 29
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.

mo.
  • 3,474
  • 1
  • 23
  • 20