Skip to content

Null reference exception thrown when using List.Add<T> #1821

Description

@VoidPlayz510

Library/API/IoT binding

nanoFramework.System.Collections

Visual Studio version

VS2026

.NET nanoFramework extension version

2.0.0-preview.15

Target name(s)

ESP32

Firmware version

N/A

Device capabilities

No response

Description

When trying to add either type double or type custom class to a list the program throws a null reference exception even if the collection itself isn't null and was defined. (Sometimes works, sometimes does not)

How to reproduce

  1. Setup the pins for RX/TX on DS18b20 device
  2. Modify the esp32 configuration to match that in program.cs
  3. Run the program

Expected behaviour

The item(s) to be added to the list without the compiler thinking the list is null

Screenshots

Image

Sample project or code

`using Iot.Device.Ds18b20;
using nanoFramework.Device.OneWire;
using nanoFramework.Hardware.Esp32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using UnitsNet;

namespace GPSMinimal;

public class Program
{
private static OneWireHost OneWire;
private static readonly object TempLock = new();

private static readonly List<double> LatestTemps = new List<double>();
private static readonly List<Ds18b20> Sensors = new List<Ds18b20>();

public static void InitialiseAll()
{
    OneWire = new OneWireHost();

    var deviceList = OneWire.FindAllDevices();

    if (deviceList == null || deviceList.Count == 0)
    {
        Console.WriteLine("No DS18B20 sensors found");
        return;
    }

    lock (TempLock)
    {
        Sensors.Clear();

        foreach (byte[] deviceAddr in deviceList)
        {
            if (deviceAddr[0] != 0x28)
                continue;

            Console.WriteLine("Creating sensor");

            var sensor = new Ds18b20(
                OneWire,
                deviceAddr,
                false,
                TemperatureResolution.VeryHigh);

            Console.WriteLine("Configuring sensor");

            sensor.IsAlarmSearchCommandEnabled = false;

            Console.WriteLine("Adding sensor");

            Sensors.Add(sensor);

            Console.WriteLine("Added sensor");
        }
    }

    Console.WriteLine($"Total sensors: {Sensors.Count}");
}

public static void GetTemperatureFromSensorsInCelcius()
{
    lock (TempLock)
    {
        LatestTemps.Clear();

        foreach (var sensor in Sensors)
        {
            if (!sensor.TryReadTemperature(out Temperature temp))
                continue;

            LatestTemps.Add(temp.DegreesCelsius);
        }
    }
}

public static List<double> GetSnapshot()
{
    lock (TempLock)
    {
        return new List<double>(LatestTemps);
    }
}

public void Dispose()
{
    if (OneWire != null)
    {
        OneWire.Dispose();
        OneWire = null;
    }
}

private static void TemperaturePollingLoop()
{
    while (true)
    {
        GetTemperatureFromSensorsInCelcius();
        var snapshot = GetSnapshot();
        foreach (var temp in snapshot)
        {
            Console.WriteLine($"Temp {temp:F2}");
        }

        Thread.Sleep(1000);
    }
}

public static void Main()
{
    Debug.WriteLine("Hello from nanoFramework!");

    // temp pins
    Configuration.SetPinFunction(27, DeviceFunction.COM3_RX);
    Configuration.SetPinFunction(27, DeviceFunction.COM3_TX);

    InitialiseAll();
    new Thread(TemperaturePollingLoop).Start();

    Thread.Sleep(Timeout.Infinite);
}

}`

Aditional information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions