0

Please help! I am getting error in the line : details.NominalVoltage = String.Join(",", paneldetails?.NominalVoltage?.ToArray());

I have below code in my builder.

foreach (var panel in panelAddresses.Take(2))
                {
                    var paneldetails = new SM_NFPA72ReportPage1();
                    details.batteryDetails = new List<Battery>();
                 
                    var AssociatedPrimaryPowers = new Repository(new BuildConnection()).GetPanelPrimarypowerDevcies(reportInput.UseroId, panel, reportInput.BuildingId, reportInput.TestSessionId[0]).Result;
                    AssociatedPrimaryPowers.ForEach(x => paneldetails?.batteryDetails?.Add(new Battery
                    {
                        NominalVoltage = deviceDetailsList?.CustomProperty?.Where(y => y.fieldName == "nominalVoltage")?.FirstOrDefault()?.Value,
                        NominalAmps = deviceDetailsList?.CustomProperty?.Where(y => y.fieldName == "nominalAmps")?.FirstOrDefault()?.Value,
                        NominalLocation = deviceDetailsList?.CustomProperty?.Where(y => y.fieldName == "disconnectLocation")?.FirstOrDefault()?.Value,
                        Protection = deviceDetailsList?.CustomProperty?.Where(y => y.fieldName == "overCurrentType")?.FirstOrDefault()?.Value,
                        ProtectionAmps = deviceDetailsList?.CustomProperty?.Where(y => y.fieldName == "overCurrentAmps")?.FirstOrDefault()?.Value,
                        ProtectionLocation = deviceDetailsList?.CustomProperty?.Where(y => y.fieldName == "powerLocation")?.FirstOrDefault()?.Value,
                    }));
                    details.NominalVoltage = String.Join(",", paneldetails?.NominalVoltage?.ToArray());
                    details.NominalAmps = String.Join(",", paneldetails?.NominalAmps?.ToArray());
                    details.NominalLocation = String.Join(",", paneldetails?.NominalLocation?.ToArray());
                    details.Protection = String.Join(",", paneldetails?.Protection?.ToArray());
                    details.ProtectionAmps = String.Join(",", paneldetails?.ProtectionAmps?.ToArray());
                    details.ProtectionLocation = String.Join(",", paneldetails?.ProtectionLocation?.ToArray());
                }

Below attached is my model for above builder:

public class SM_NFPA72ReportPage1 : IReportModel
    {
        public string NominalVoltage { get; set; }
        public string NominalAmps { get; set; }
        public string NominalLocation { get; set; }
        public string Protection { get; set; }
        public string ProtectionAmps { get; set; }
        public string ProtectionLocation { get; set; }
        public List<Battery> batteryDetails { get; set; }
        public List<PanelDetailsInfo> panelInfo { get; set; }
    }

I am reusing the Battery model to fetch the values from repository

 public class Battery
    {
        public string NominalVoltage { get; set; }
        public string NominalAmps { get; set; }
        public string NominalLocation { get; set; }
        public string Protection { get; set; }
        public string ProtectionAmps { get; set; }
        public string ProtectionLocation { get; set; }
    }
8
  • Please copy here the error message you see and indicate when you see it (build vs run). if it fail at runtime, also include an example of the data. Commented Mar 21, 2022 at 14:26
  • Please provide enough code so others can better understand or reproduce the problem. Commented Mar 21, 2022 at 14:26
  • 1
    What happens if paneldetails or paneldetails.NominalVoltage is null? In that case, you'll call string.Join(", ", null), but that's not valid -- you can't join null together. You'll need to decide what you want to do if it is null: do you want an empty string, null, something else? Commented Mar 21, 2022 at 14:28
  • 1
    @Loda : Title of this question has exception i got. Commented Mar 21, 2022 at 14:28
  • 1
    That said, you're taking something which isn't an array, such as string NominalVoltage, and calling ToArray() on it. This will work, but it will split on every character, so your e.g. calling string.Join(", ", "Test".ToArray()) will result in "T,e,s,t". Are you sure that this is what you want? Commented Mar 21, 2022 at 14:29

1 Answer 1

1

The exception tells you that the parameter value is null, that should mean that:

paneldetails?.NominalVoltage?.ToArray()

...gives you a null result, and that the string.Join method does not accept it. You need to make sure that you do not provide a null value to the method.

This can be achieved in multiple ways, for example by checking for null value before calling the method:

if (panelDetails?.NominalVoltage != null)
{
    details.NominalVoltage = String.Join(",", paneldetails.NominalVoltage.ToArray());
}

or by returning a empty array by default if it is null:

details.NominalVoltage = String.Join(",", paneldetails?.NominalVoltage?.ToArray() ?? Array.Empty<string>());
Sign up to request clarification or add additional context in comments.

1 Comment

NominalVoltage = deviceDetailsList?.CustomProperty?.Where(y => y.fieldName == "nominalVoltage")?.FirstOrDefault()?.Value, I think this line of code is not returning value. Instead of using FirstOrDefault() is there any other function which provide mutiple values??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.