Null object exception

I have a strange issue that I cant debug. My application on the server is processing 100's of records and randomly I have an exception thrown.

CandidateNumber is a ValueObject (Value being a string) and we assign this from the CandNo which is an nullable short

The error I am getting is:

System.InvalidOperationException: Nullable object must have a value.
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Nullable`1.get_Value()
   at Interchange.Domain.Candidates.BaseInterchangeCandidate.ToCandidate() in c:\BuildAgent\work\4d89ab20b9f76f2d\InterchangeShared\Interchange.Domain\Candidates\BaseInterchangeCandidate.cs:line 147

But I cant for the life of me work out why this exception is thrown

Many thanks

Richard




public short? CandNo { get; set; }
 
...
line 147:    candidate.CandidateNumber = new Entries.ValueObjects.CandidateNumber() { Value = CandNo == null ? String.Empty : CandNo.Value.ToString() };
 
public CandidateNumber CandidateNumber { get; set;}

Open in new window

rparkinsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MrJesse34Commented:
In the constructor for the public "CandidateNumber", have code like this:

get {
if (Value == CandNo == null){
  return  String.Empty;
}else {
    return CandNo.Value.ToString() ;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MrJesse34Commented:
I'm sorry, no caps on the "value" and that code should read like this:


get {
if (CandNo == null){
  return  String.Empty;
}else {
    return CandNo.Value.ToString() ;
}

Open in new window

0
rparkinsAuthor Commented:
Hi MrJesse34 many thanks for your solution, it does seem strange how this only happens a few times, I just wondered if there was something inhernetly wrong with the code. We have placed this through reflector and the processor is checking HasValue on both the Object and the short so I cannot see how this would go wrong for any instance of values.. I will try your solution but it is a "change and see" kind of solution as I cant see why the original is throwing a null exception

Many thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.