0

I have a power unit enumeration type that looks like as below.

public sealed record PowerUnit : StringEnumeration
{
    public static readonly PowerUnit BTUH = new(nameof(BTUH), UnitConstants.BritishThermalUnitsPerHour);
    public static readonly PowerUnit WATT = new(nameof(WATT), UnitConstants.Watt);

    private static readonly Dictionary<string, PowerUnit> _byNameDictionary = GetByNameDictionaryInternal<PowerUnit>();
    public static PowerUnit ByName(string name) => _byNameDictionary[name];

    private PowerUnit() { }
    public PowerUnit(string id, string name) : base(id, name) { }

    public static PowerUnit FromUnitsNetUnit(UnitsNet.Units.PowerUnit unitsNetUnit)
        => unitsNetUnit switch
        {
            UnitsNet.Units.PowerUnit.BritishThermalUnitPerHour => BTUH,
            UnitsNet.Units.PowerUnit.Watt => WATT,
            _ => throw new ArgumentException("Invalid unit type.", nameof(unitsNetUnit))
        };

    // The following is internal because if 'public' it causes HC pipeline initialization to crash
    internal UnitsNet.Units.PowerUnit ToUnitsNetUnit()
        => this.Id switch
        {
            nameof(BTUH) => UnitsNet.Units.PowerUnit.BritishThermalUnitPerHour,
            nameof(WATT) => UnitsNet.Units.PowerUnit.Watt,
            _ => throw new InvalidOperationException("The equivalent UnitsNet unit is unknown.")
        };
}

I have a power class that looks like as below.

public class Power : UnitType<Power, PowerUnit>, IConvertibleUnit<Power, PowerUnit>
{
    public Power() { }
    public Power(Power source) : base(source){ }
    public Power(PowerUnit unit, double scalar) : base(unit, scalar) {}

    public override Power Convert(PowerUnit toUnit)
    {
        if (toUnit == this.Unit) return new(this);

        UnitsNet.IQuantity powerQuantity = UnitsNet.Power.From(Scalar, Unit.ToUnitsNetUnit()); // Getting exception here and Unit is null
        var toScalar = powerQuantity.ToUnit(toUnit.ToUnitsNetUnit()).Value;

        return new(toUnit, toScalar);
    }
}

Here UnitType is an abstract class, and its signature looks like as below,

public abstract class UnitType<T0, T1> where T0: class, IConvertibleUnit<T0, T1>
    where T1: StringEnumeration
{
    public string UnitId { get; set; }
    public T1 Unit { get; set; }
    public double Scalar { get; set; }

    protected UnitType() { }
    protected UnitType(UnitType<T0, T1> source)
    {
        UnitId = source.UnitId;
        Unit = source.Unit;
        Scalar = source.Scalar;
    }

    protected UnitType(T1 unit, double scalar)
    {
        UnitId = unit.Id;
        Unit = unit;
        Scalar = scalar;
    }

    public abstract T0 Convert(T1 toUnit);
}

I am using this power as a column type, and one of the tables that looks like as below,

public class Space 
{
    //.....
    public Power ElectricalPowerOverride { get; set; }
   //....       
 }

I have a requirement where I need to get the scalar value from this ElectricalPowerOverride for that I am accessing the same value as below,

var dhSpace = _dbContext.Spaces.SingleOrDefault(space => space.ProjectNumber == _designHubProject.ProjectNumber);
var electricalPowerLevelInSI = dhSpace.ElectricalPowerOverride.Convert(PowerUnit.WATT).Scalar; // here getting that null reference exception when accessing the scalar

Could anyone please let me know why I am getting the exception inside the Convert method, like getting object null for Unit when trying to access Unit.ToUnitsNetUnit() in Power class

Sorry for the long post, As those are necessary for the problem

Many thanks in advance!!!

Glory Raj
  • 17,397
  • 27
  • 100
  • 203
  • Have you used the debugger? Are you sure that not `dhSpace` is `null` but `Convert` returns `null`? The latter should not be possible, the former is a valid use-case. – Tim Schmelter Sep 21 '22 at 16:36
  • There's way too much going on in your code, please create a [mre] and narrow down what is null and where. See duplicate, set breakpoints, step through your code and inspect your variables. – CodeCaster Sep 21 '22 at 16:37
  • Yeah, Getting null reference exception at this line `UnitsNet.IQuantity powerQuantity = UnitsNet.Power.From(Scalar, Unit.ToUnitsNetUnit());` and looks like `Unit` is null – Glory Raj Sep 21 '22 at 16:37
  • @TimSchmelter I am sure `dhSpace` is not null here – Glory Raj Sep 21 '22 at 16:38
  • @GloryRaj: so you get two different NRE? One where you comment "here getting that null reference exception when accessing the scalar" and one where you comment "Getting exception here and Unit is null"? – Tim Schmelter Sep 21 '22 at 16:39
  • @TimSchmelter Sorry for the confusion; code related to `dhspace` is an API, and the other code is in the local package library, and when I debug, I found out that the actual power class convert method has a problem – Glory Raj Sep 21 '22 at 16:42

0 Answers0