When executing a foreach searching for null values it throws an exception instead of executing the code specified for null-objects.
foreach (Requirement requirement in DBRequirements)
{
if (this.Repository.GetElementByGuid(requirement.Guid) == null)
{
hasChanges = true;
requirement.IsDeleted = true;
}
}
Repository.GetElementByGuid()
is a function from Enterprise Architect. I can't modify this and thus I'll have to assume when not found it immediately throws an exception instead of returning null
? (Haven't been able to find out for sure, documentation is kind of scarce on this.) I'm thinking about using a try catch
as if else
but I'm wondering if anyone has a better suggestion.
Basically what's supposed to happen; it checks every requirement in the DB if it's Enterprise Architect-counterpart still exists. If it doesn't, the DB should mark the current requirement as IsDeleted
, if it does nothing needs to be done. It's part of a synchronization cycle to update the DB.
Guid is a unique identifier within Enterprise Architect.
Edit: Forgot to actually mention the exception.. It's throwing the exception on the if-statement as a:
NullReferenceException
...on GetElementByGuid(requirement.Guid)
, stating that no such element exists in the current context.
For clarification;
DBRequirements is pulled straight from the database and checked if not empty, requirement.Guid can't be null because it's the primary-key in the database (and thus NOT NULL), the data-base record could simply not exist without it. This.Repository is declared in the constructor (checked with debugger in runtime, it's also not null
).
This is why I'm unable to find out what is throwing the exception. Something returns to be null
, but nothing is within this foreach actually does have a null
-value.