0

I try this statement to check if SSN is not empty return the value in SSNstr otherwise return null. But this is returning me error Object reference not set to an instance of an object. when SSN is empty.

  Model.DocumentData dt = new Model.DocumentData();     
 dt.SSNstr = (dt.SSNstr == null ? null :(string)individual.XPathSelectElement("Individual/SSN")).Insert(5,"-").Insert(3,"-");

this is dt class:

      [DataMember]
      public string SSNstr = string.Empty;

Please attention here I know how to check null value but I like to do it in same line of code. As I have so many fields like this that I need to check I prefer to check in 1 line of the code or change something in dt class that take care of it.

Alma
  • 2,248
  • 9
  • 34
  • 66

5 Answers5

7

Null and "empty string" are two different things. Make your check with String.IsNullOrEmpty or String.IsNullOrWhiteSpace.

At the end of that, the result of your XPath query might be empty. Why don't you break that up into a few lines?

Addendum: It really looks like your XPath query result is empty. You're working on the assumption that it will not be empty. Do you have that assurance? When you're programming, don't trust anyone. Including yourself.

MPelletier
  • 15,130
  • 14
  • 79
  • 128
  • Thanks just because it is too many fields like 50, I wanted to make each in 1 line so make it less code and cleaner. Si it any possibility I can check all in this line? – Alma Jun 23 '15 at 18:19
  • 1
    @Alma, let me put it this way: Whether it's one field or 500, you're not saving time if your expression stands on one line but is too complex to debug. Visually, it's a bit confusing. – MPelletier Jun 23 '15 at 18:30
  • 3
    If "fewer lines = cleaner code" were true, we'd all be posting serious questions on codegolf.SE – Mathias R. Jessen Jun 23 '15 at 18:33
  • 1
    @Alma you can always extract a single line method to place in all 50 spots once you have the method logic complete. – hellyale Jun 23 '15 at 18:49
2

Check your parentheses.

If dt.SSNstr is null, the expression is not returning null, it is returning (null).Insert(5,"-").Insert(3,"-") which causes the error. You can't insert anything into null.

One solution would be to write

dt.SSNstr = dt.SSNstr == null ? null :(string)individual.XPathSelectElement("Individual/SSN").Insert(5,"-").Insert(3,"-");

but then if would be better to have

if (dt.SSNstr!=null)
  dt.SSNstr =(string)individual.XPathSelectElement("Individual/SSN").Insert(5,"-").Insert(3,"-");

otherwise you're assigning null to a variable that is already null.

Edit: As the other answers say, you should check for IsNullOrEmpty, not just equal to null. You start out with string.Empty, but later you assign null to it.

Mr Lister
  • 42,557
  • 14
  • 95
  • 136
1

dt might be null in this case. I would check if that object actually has a value. You would need to instantiate dt, then you can set the value of dt.SSNstr.

Batman
  • 541
  • 4
  • 24
  • 1
    I instantiate dt as Model.DocumentData dt = new Model.DocumentData(); and ssn as [DataMember] public string SSNstr= string.Empty; – Alma Jun 23 '15 at 18:21
1

Per your edit, you can simply do like

    dt.SSNstr = ((individual.XPathSelectElement("Individual/SSN") as string) == null) ? null : (individual.XPathSelectElement("Individual/SSN") as string).Insert(5,"-").Insert(3,"-");
Rahul
  • 71,392
  • 13
  • 57
  • 105
1

var test = "";

var a = string.IsNullOrEmpty(test) ? null : test;

I hope it´s Helps.

Andre Mendonca
  • 516
  • 5
  • 11