2

I have a program in which I keep getting the NullReferenceException error. Here is the code that is causing the error:

string description = "";
if (string.IsNullOrEmpty(eventItem.Description.ToString()))
{
     description = "No description available.";
}
else
{                        
     description = eventItem.Description.ToString();  
}

I have looked through this post (What is a NullReferenceException and how do I fix it?), and I have tried several of the solutions (I'm afraid I simply don't understand all of them enough to try them), but I just can't figure out why this is happening. In my understanding, this error occurs because the string is in fact null.

There are events on my Google Calendar that have no description entered, so the description is null, but shouldn't the code I have check for that, and handle it? Or, is the problem that eventItem.Description.ToString() cannot be null when I call the IsNullOrEmpty method? I have also tried changing the if statement to this:

if (eventItem.Description.ToString() == null)

...but I still get the NRE error. I have tried rewriting my code so many different ways, but nothing has worked. I'm at the end of my rope!

Duders
  • 83
  • 9

2 Answers2

1

Your eventItem itself may be null. Do something along the lines

 if ( eventItem!= null && eventItem.Description != null && eventItem.Description.ToString() == null)

Updated after DStanley pointed out in the comment that .ToString() == null comparison would be unnecessary.

if ( eventItem!= null && eventItem.Description != null)
{
}

Just to help you understand a little better... NullReferenceException basically means you are trying to use some object without instantiating it. One of the simplest way to prevent is to add a null check if you are not sure whether the object is not null. And when I say adding a null check, it simply means comparing to null in the if block before accessing any property of the object.

if( objectName != null)
{
//then do something on the object

}
haku
  • 4,105
  • 7
  • 38
  • 63
  • 3
    Or `eventItem.Description` could be null. – Jack A. Apr 01 '16 at 02:21
  • Yes. Thank-you, Jack. I updated the answer. Without the information you gave, it may have still thrown the error. – haku Apr 01 '16 at 02:25
  • 1
    `ToString` will not return null for any framework types. Unless `Description` is a custom type that overloads `ToString` to return `null` for a valid value (which would be very strange) the check for `ToString` to be null is unnecessary. – D Stanley Apr 01 '16 at 02:33
  • @DStanley - thanks for pointing that out. Indeed that .ToString() comparision to null will be unnecessary, unless overloaded. – haku Apr 01 '16 at 02:37
  • Thank you so much! I just removed the .ToString() from that line, and it worked perfectly. – Duders Apr 01 '16 at 03:26
0

shouldn't the code I have check for that, and handle it?

Not if item or item.Description is null. If item is null then the call to .Description will throw a null reference exception, and if item.Description is null then the call to ToString will throw a null reference exception. There's no "magic" that lets you call ToString on a null reference.

Note that if item.Description is already a string then there's no need to call ToString(). just do:

if (string.IsNullOrEmpty(eventItem.Description))
{
     description = "No description available.";
}
else
{                        
     description = eventItem.Description;  
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • if `eventItem.Description` is null it again Throws the same exception; since `.ToString()` will not convert `null` to `""` – sujith karivelil Apr 01 '16 at 02:33
  • @un-lucky Thanks - I forgot to edit that part of my answer. – D Stanley Apr 01 '16 at 02:34
  • Wouldn't you want to check null for the 'eventItem' object before accessing the 'Description' property? – haku Apr 01 '16 at 02:46
  • Sure, unless it's checked for `null` somewhere else in the code. The description of the program leads me to assume that the `eventItem` is used elsewhere, so it may have already been checked for null. – D Stanley Apr 01 '16 at 02:51
  • That's correct. I query the calendar, and then perform the code using the following if statement: if (events.Items != null && events.Items.Count > 0) – Duders Apr 01 '16 at 23:26