Null object exception

Posted on 2009-05-27
Medium Priority
Last Modified: 2012-05-07
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


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

Question by:rparkins
  • 2

Accepted Solution

MrJesse34 earned 750 total points
ID: 24484115
In the constructor for the public "CandidateNumber", have code like this:

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

Expert Comment

ID: 24484129
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


Author Comment

ID: 24491396
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…
Through the video, you can check the migration process of Outlook PST file to PDF. Kernel for Outlook to PDF tool can convert Outlook emails with all attributes like Subject, To, From, Cc, Bcc and other folders such as Inbox, Outbox, Sent Items, Jun…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question