babalae/better-genshin-impact · error · ArgumentNullException

内部视图模型对象为空

Error message

内部视图模型对象为空

What it means

Thrown as ArgumentNullException when App.GetService<TaskSettingsPageViewModel>() returns null inside AutoFishing. This means the WPF dependency-injection container either has not registered TaskSettingsPageViewModel or is not available in the current execution context (e.g., running outside the main application lifecycle).

Source

Thrown at BetterGenshinImpact/Core/Script/Dependence/Genshin.cs:462

    /// <summary>
    /// 返回主界面
    /// </summary>
    /// <returns></returns>
    public async Task ReturnMainUi()
    {
        await new ReturnMainUiTask().Start(CancellationContext.Instance.Cts.Token);
    }

    /// <summary>
    /// 钓鱼
    /// </summary>
    /// <returns></returns>
    public async Task AutoFishing(int fishingTimePolicy = 0)
    {
        var taskSettingsPageViewModel = App.GetService<TaskSettingsPageViewModel>();
        if (taskSettingsPageViewModel == null)
        {
            throw new ArgumentNullException(nameof(taskSettingsPageViewModel), "内部视图模型对象为空");
        }

        var param = AutoFishingTaskParam.BuildFromConfig(TaskContext.Instance().Config.AutoFishingConfig, taskSettingsPageViewModel.SaveScreenshotOnKeyTick);
        param.FishingTimePolicy = (FishingTimePolicy)fishingTimePolicy;
        await new AutoFishingTask(param).Start(CancellationContext.Instance.Cts.Token);
    }

    /// <summary>
    /// 重新登录原神
    /// </summary>
    /// <returns></returns>
    public async Task Relogin()
    {
        await new ExitAndReloginJob().Start(CancellationContext.Instance.Cts.Token);
    }
    
    /// <summary>
    /// 进出千星奇域

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Ensure AutoFishing is only called after the WPF application has fully initialized and the ServiceProvider is available.
  2. Verify TaskSettingsPageViewModel is registered in App.xaml.cs services.AddView or services.AddTransient.
  3. If running in a non-WPF context, inject the ViewModel or the required config value directly instead of resolving from App.GetService.
  4. Check that the service provider hasn't been disposed (e.g., during shutdown) before calling.

Example fix

// before
public async Task AutoFishing(int fishingTimePolicy = 0)
{
    var vm = App.GetService<TaskSettingsPageViewModel>();
    if (vm == null)
        throw new ArgumentNullException(nameof(vm), "内部视图模型对象为空");
    // ...
}

// after — fail with a user-actionable message and early-return instead of null-deref
public async Task AutoFishing(int fishingTimePolicy = 0)
{
    var vm = App.GetService<TaskSettingsPageViewModel>()
        ?? throw new InvalidOperationException("AutoFishing 不可用:任务设置页面未初始化,请确保调度器已加载完毕。");
    // ...
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Check service availability before calling AutoFishing
var vm = App.GetService<TaskSettingsPageViewModel>();
if (vm == null)
{
    _logger.LogError("TaskSettingsPageViewModel not available — AutoFishing requires full app initialization");
    return;
}

Try / catch

try
{
    await genshin.AutoFishing(fishingTimePolicy);
}
catch (ArgumentNullException ex) when (ex.ParamName == "taskSettingsPageViewModel")
{
    _logger.LogError("AutoFishing unavailable: DI container not initialized. Ensure the app is fully started.");
}

Prevention

When it happens

Trigger: Calling genshin.AutoFishing() when the application's ServiceProvider has not been initialized (headless test, plugin context, or before App startup completes). Also possible if TaskSettingsPageViewModel was removed from DI registration or the service scope is disposed.

Common situations: Running a script during application startup before the DI container is fully built. Unit testing AutoFishing without mocking App.GetService. A refactor that accidentally removed the TaskSettingsPageViewModel registration from App.xaml.cs.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/a85f0a7e31300dcb. Report an issue: GitHub.