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.

6
  • either dt or individual is null check them. like dt!=null && dt.SSNstr==null...something like that Commented Jun 23, 2015 at 18:17
  • 1
    Also, XPathSelectElement cound be returning null (causing the chained Insert to throw an exception). Commented Jun 23, 2015 at 18:18
  • If it is null you cannot do an Insert on it. Commented Jun 23, 2015 at 18:20
  • 1
    possible duplicate of What is a NullReferenceException and how do I fix it? Commented Jun 23, 2015 at 18:27
  • Have you tried debugging this problem? Commented Jun 23, 2015 at 18:28

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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, 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.
If "fewer lines = cleaner code" were true, we'd all be posting serious questions on codegolf.SE
@Alma you can always extract a single line method to place in all 50 spots once you have the method logic complete.
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.

2 Comments

I tried different way of putting parentheses but it producing error. can you say what should I change?
Remove the first ( and the second )). I'll edit the answer.
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.

1 Comment

I instantiate dt as Model.DocumentData dt = new Model.DocumentData(); and ssn as [DataMember] public string SSNstr= string.Empty;
1

var test = "";

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

I hope it´s Helps.

2 Comments

XPathSelectElement returns an XElement that does not have an Insert method. You need a .Value before the first Insert.
You're right, I copied his example of it. I changed to a simple example. TKS
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,"-");

1 Comment

@Alma, that's because of extra parenthesis (. edited the answer; should be fine now.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.