I seem to be getting an unexplained System.InvalidOperationException stating that a "Nullable object must have a value" at the point when I'm checking the value of the object for null. Unfortunately I'm only receiving this randomly every 1 out of 1,000,000 messages and cant seem to replicate in my local environment with a simulator, which is why I've turned to you guys for help.
I have a foreach loop processing an array of objects:
ReceivedMessage[] messages = pushFeed.ReceivedMessages.GetOustandingMessages();
foreach (var m in messages)
{
if (m != null) // only here as a sanity check to ensure m was not somehow null
{
if(pushFeed.Feed.ForwardReceivedMessage(m))
logger.Info("Successfully posted to server");
else
logger.Warn("Failed to post to server");
}
}
Inside ForwardReceivedMessage
I'm then checking if the Source
object within the message object is null and thats where I seem to be getting the InvalidOperationException.
public bool ForwardReceivedMessage(ReceivedMessage message)
{
CommToken token;
if (message.Source == null) // This is the line where the exception is being thrown...?
{
logger.Warn("Failed to forward feed with null Source...");
return false;
}
// Do other things...
return true;
}
The ReceivedMessage
object is simply:
public class ReceivedMessage
{
public long MessageId;
public MessageBase Payload;
public IAsset Source;
}
And the Source
object is just an interface with the following:
public interface IAsset
{
int ClientId { get; }
ushort CommId { get; }
int DeviceId { get; }
short DeviceMode { get; set; }
DateTime? LastReportTime { get; }
string Name { get; }
bool? Panic { get; }
int? PassthroughDeviceId { get; set; }
DeliveryMethod PrimaryMTDeliveryMethod { get; }
DeliveryMethod? SecondaryMTDeliveryMethod { get; }
// ... etc, etc
}
As far as I can see, the only issue can be with the Source
object, and simply calling message.Source
to check if it is null seems to be what is throwing the InvalidOperationException.
I'm thinking it may have something to do with Source
being declared as the interface IAsset
as opposed to the object, however even if Source
were never assigned I believe I would be able to check that is has a value in an if statement?
Im hoping someone here can explain how/why this would happen, and also provide a solution to how I can check if Source
has value or not.
Edit: The full exception printout which I'm getting is:
System.InvalidOperationException: Nullable object must have a value.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at CompanyName.Division.MessageHandler.Platform.PlatformHandler.ForwardReceivedMessage(ReceivedMessage message) in c:\Git\message-handler\MessageHandler\PlatformFeeds\CompanyName.Division.MessageHandler.Platform\PlatformHandler.cs:line 37
at CompanyName.Division.MessageHandler.Core.FeedManager.<SendCallback>d__11.MoveNext() in c:\Git\message-handler\MessageHandler\CompanyName.Division.MessageHandler.Core\FeedManager.cs:line 160