1

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.

Ciphra
  • 269
  • 2
  • 17
  • what is the exception, is it null reference exception ? – mybirthname Oct 12 '16 at 06:56
  • Maybe `this.Repository == null`? – MickyD Oct 12 '16 at 06:59
  • or `requirement` could be null. We need the exception and some context – David Pilkington Oct 12 '16 at 07:00
  • Sorry, forgot to actually mention the exception, edit on main post – Ciphra Oct 12 '16 at 07:01
  • @MickyD Repository is declared in constructor so can't be null either (and is not null, checked with debugger) – Ciphra Oct 12 '16 at 07:04
  • @Ciphra My best bet is: one of the internals of Repostiory is null (or has become null). StackTrace in the exception usually shows where the exception comes from! – Flying Dutch Boy Oct 12 '16 at 07:06
  • There's a difference between older versions and newer in the way the operation `Repository.GetElementByGUID` behaves. It used to throw an Exception if the element was not found, but recently (version 12 IIRC) it returns null. Anyway, put the piece of code in a try-catch block to make sure. – Geert Bellekens Oct 12 '16 at 09:58
  • @Chipra Yes it wont throws exception now as Geert mentioned. Here your condition seems to be wrong .Change it to not equal to null (!=) from (==) .Hope your exceptions is thrown from requirement.Isdeleted line because as your condition even if requirement is not found youre trying to et that bool value. – Dah Sra Oct 12 '16 at 11:14
  • @GeertBellekens I'll put it in a try-catch to use as a work-around for now, I'll keep looking for a more clear and 'acceptable' solution. – Ciphra Oct 12 '16 at 12:52
  • @dahsra that would do the opposite of what I'm trying to do :^). That'd make it so that if the requirement I have in the database DOES exist in EA it will remove it from the DB. – Ciphra Oct 12 '16 at 12:53

2 Answers2

3

try this where you only use DBRequirements that have Guids

foreach (Requirement requirement in DBRequirements.Where(r => r?.Guid != null))
{
    if (this.Repository.GetElementByGuid(requirement.Guid) == null)
    {
        hasChanges = true;
        requirement.IsDeleted = true;
    }
}
David Pilkington
  • 13,043
  • 3
  • 36
  • 65
  • Guid can't be null in the database as it's the primary key. – Ciphra Oct 12 '16 at 07:03
  • 1
    This also protects the case if the `requirement` is null – David Pilkington Oct 12 '16 at 07:09
  • afaik, but I could be wrong in understanding this, it's not possible for any of the `requirement`s in `DBRequirements` to be `null`, as they are pulled straight from the DB (in the DB a record can't be `null`, it MUST have at least the `Guid`) and it's treated (not set) as a `readonly` and thus never modified from there on. – Ciphra Oct 12 '16 at 07:13
1

Try the below code..

foreach (Requirement requirement in DBRequirements)
{
    if (requirement != null)
    {
        if (this.Repository.GetElementByGuid(requirement.Guid) == null)
        {
            hasChanges = true;
            requirement.IsDeleted = true;
        }
    }
}
Balagurunathan Marimuthu
  • 2,666
  • 4
  • 23
  • 37