Skip to content

Quality: Potential NullReferenceException in BaseSettings Constructor#1212

Merged
Spiritreader merged 2 commits intoAutoDarkMode:v11.1from
bachdev:contribai/improve/quality/potential-nullreferenceexception-in-base
Apr 9, 2026
Merged

Quality: Potential NullReferenceException in BaseSettings Constructor#1212
Spiritreader merged 2 commits intoAutoDarkMode:v11.1from
bachdev:contribai/improve/quality/potential-nullreferenceexception-in-base

Conversation

@bachdev
Copy link
Copy Markdown

@bachdev bachdev commented Apr 1, 2026

Problem

The constructor of BaseSettings<T> uses Activator.CreateInstance(typeof(T)) to initialize the Component property. If T does not have a parameterless constructor or if T is an interface or abstract class, this will throw an exception at runtime, potentially causing a crash.

Severity: high
File: AutoDarkModeLib/ComponentSettings/BaseSettings.cs

Solution

Add a constraint to ensure T has a parameterless constructor by using where T : new() in the class definition. This will enforce compile-time checks and prevent runtime exceptions.

Changes

  • AutoDarkModeLib/ComponentSettings/BaseSettings.cs (modified)

Testing

  • Existing tests pass
  • Manual review completed
  • No new warnings/errors introduced

The constructor of `BaseSettings<T>` uses `Activator.CreateInstance(typeof(T))` to initialize the `Component` property. If `T` does not have a parameterless constructor or if `T` is an interface or abstract class, this will throw an exception at runtime, potentially causing a crash.


Affected files: BaseSettings.cs

Signed-off-by: BachDEV <1437214+bachdev@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 1, 2026 00:23
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens BaseSettings<T> initialization by eliminating runtime Activator.CreateInstance failures and moving instantiation requirements to compile time.

Changes:

  • Add where T : new() constraint to BaseSettings<T> to require a public parameterless constructor.
  • Replace Activator.CreateInstance(typeof(T)) with new T() in the constructor.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

namespace AutoDarkModeLib.ComponentSettings;

public class BaseSettings<T> : ISwitchComponentSettings<T>
public class BaseSettings<T> : ISwitchComponentSettings<T> where T : new()
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BaseSettings<T> now requires where T : new(), but derived generic types must also guarantee that constraint. BaseSettingsEnabled<T> : BaseSettings<T> currently has no new() constraint, which will cause a compile-time error. Update BaseSettingsEnabled<T> (and any other subclasses) to include where T : new() or otherwise satisfy the base constraint.

Copilot uses AI. Check for mistakes.
namespace AutoDarkModeLib.ComponentSettings;

public class BaseSettings<T> : ISwitchComponentSettings<T>
public class BaseSettings<T> : ISwitchComponentSettings<T> where T : new()
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding where T : new() to this public generic type is a source/binary breaking change for any external callers that previously used BaseSettings<T> with types lacking a public parameterless constructor (or abstract/interface types). If avoiding a breaking change is important, consider keeping the unconstrained type parameter and handling instantiation failures explicitly (e.g., custom exception/message) instead of enforcing a new() constraint.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

@bachdev
Copy link
Copy Markdown
Author

bachdev commented Apr 1, 2026

@copilot apply changes based on the comments in this thread

@Spiritreader Spiritreader changed the base branch from master to v11.1 April 9, 2026 19:19
@Spiritreader Spiritreader merged commit 8d17a34 into AutoDarkMode:v11.1 Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants