Developer forum

Forum » Development » ManufacturerIDValueMapper null reference

ManufacturerIDValueMapper null reference

Arnór Halldórsson
Arnór Halldórsson
Reply

Yo,

I noticed a thing that happens when the ProductManufacturerId column in EcomProducts is a whitespace (or has a whitespace) in it.

Manufacturer.GetManufacturerById() returns null (because of the whitespace) so the code inside ManufacturerIDValueMapper.MapValues() returns a null reference when trying to read the id for the given manufacturer.

Changing string.IsNullOrEmpty to string.IsNullOrWhitespace inside ManufacturerIDValueMapper.MapValues() and Manufacturer.GetManufacturerById() and adding null checks (? behind the object reference) should fix this:

    public class ManufacturerIDValueMapper : ValueMapperBase
    {
        public override IEnumerable<FieldValueMapping> MapValues(IEnumerable<string> terms)
        {
            var mappings = new List<FieldValueMapping>();
            foreach (var term in terms)
            {
                var manufacturerItem = Manufacturer.GetManufacturerById(term);
                if (!string.IsNullOrEmpty(manufacturerItem.Id))
                if (!string.IsNullOrWhiteSpace(manufacturerItem?.Id))
                {
                    mappings.Add(new FieldValueMapping() { Key = manufacturerItem.Id, Value = manufacturerItem.Name });
                }
            }

            return mappings;
        }
    }

and

        public static Manufacturer GetManufacturerById(string id)
        {
            Manufacturer manufacturer = null;
            if (!string.IsNullOrEmpty(id) && CachedManufacturers.TryGetValue(id, out manufacturer))
            if (!string.IsNullOrWhiteSpace(id) && CachedManufacturers.TryGetValue(id, out manufacturer))
            {
                return manufacturer;
            }

            return null;
        }

Best regards,
Arnór

GetManufacturerById.png ManufacturerIDValueMapper.png

Replies

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Awesome!

Will get it into our codebase.

Thanks a bunch!

BR Nicolai

Votes for this answer: 2
 
Arnór Halldórsson
Arnór Halldórsson
Reply

My hero!

 
Oleg Rodionov Dynamicweb Employee
Oleg Rodionov
Reply

Hi,

The issue is now bugged by 4677. Thanks for finding.

BR, Oleg QA 

 
Kristian Kirkholt Dynamicweb Employee
Kristian Kirkholt
Reply

Hi Arnòr

Bugfix on #4677 ManufacturerIDValueMapper null reference

Has been fixed in Dynamicweb version 9.10.15

Get this version from download section https://doc.dynamicweb.com/downloads/releases

Please contact Dynamicweb Support if there are any problems or questions regarding the update

Kind Regards
Dynamicweb Support
Kristian Kirkholt

 

 

You must be logged in to post in the forum