-2

I have this object:

var obj = JsonConvert.DeserializeObject<RootObject>(responseText);

now in some case the deserialization generate two key: arts and det. The det key is even filled, but in some cases the key arts could be null. I check the object content null like this:

foreach(var item in obj.det){
   ...
   if(!item.arts.Equal(null)){ 'the problem is here
    ...
   }
}

The problem is on the condition, in particular I check if the arts key is different against null but I got this exception:

NullReference Exception was not managed

I don't understand what I did wrong, could someone enlight me?

Sandokan
  • 1,075
  • 3
  • 15
  • 30
  • 2
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Silvermind Apr 06 '16 at 16:50
  • @Silvermind I already checked it, and there is a suggestion to use `Equals`. – Sandokan Apr 06 '16 at 16:52
  • Your statement in your comment is incorrect. Check the examples that specify the correct implementation. You should use operators like `==` for `null` comparison. – Silvermind Apr 06 '16 at 16:55

1 Answers1

3

Try

if(item.arts == null){
 // do your checking operation
}

I am not sure if that is causing your problem but in general calling a method on a null object creates an error.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • @nhouser9 Yes, seems working good thanks. So I guess that `Equals` method not working in this case if the object is null. – Sandokan Apr 06 '16 at 16:51
  • @Sandokan No problem. In general, if an object is null you can't call one of its methods. If this answer solved your issue, please mark it as a solution =) – nhouser9 Apr 06 '16 at 16:55