© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•8mo ago•
12 replies
Core

✅ Field is not initialized by the source generator: System.NullReferenceException

Hello,
I have a source generator that should initialize a field with an empty value.
The source generator is in
UaDetector.SourceGenerator
UaDetector.SourceGenerator
, and it's used by
UaDetector
UaDetector
.The generation succeeds when
UaDetector
UaDetector
project is build.
When running a project that uses UaDetector, a NullReferenceException is thrown.

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.


The source generator initializes any field that uses the
Regexes
Regexes
attribute. (currently with an empty list).
namespace UaDetector.Parsers;

public sealed partial class BrowserParser : IBrowserParser
{
    [Regexes("Regexes.Resources.Browsers.browsers.json")]
    internal static readonly IReadOnlyList<Browser> Browsers;
}
namespace UaDetector.Parsers;

public sealed partial class BrowserParser : IBrowserParser
{
    [Regexes("Regexes.Resources.Browsers.browsers.json")]
    internal static readonly IReadOnlyList<Browser> Browsers;
}


The following code is generated:
using System.Collections.Frozen;

namespace UaDetector.Parsers;

public partial class BrowserParser
{
    static BrowserParser()
    {
        Browsers = new global::UaDetector.Regexes.Models.Browsers.Browser[] { };
    }
}
using System.Collections.Frozen;

namespace UaDetector.Parsers;

public partial class BrowserParser
{
    static BrowserParser()
    {
        Browsers = new global::UaDetector.Regexes.Models.Browsers.Browser[] { };
    }
}


UaDetector
UaDetector
references the surce generator the following way:
<ProjectReference Include="..\UaDetector.SourceGenerator\UaDetector.SourceGenerator.csproj"
                  OutputItemType="Analyzer"
                  ReferenceOutputAssembly="false" />
<ProjectReference Include="..\UaDetector.SourceGenerator\UaDetector.SourceGenerator.csproj"
                  OutputItemType="Analyzer"
                  ReferenceOutputAssembly="false" />
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

System.NullReferenceException
C#CC# / help
2y ago
❔ System.NullReferenceException
C#CC# / help
3y ago
✅ System.NullReferenceException
C#CC# / help
3y ago
❔ Field / Property Initialized By Method
C#CC# / help
3y ago