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

Hi, I’m trying to convert my .net TestProject Android automation project to use SpecFlow, however I see a NullReferenceException when creating the driver on this line:

mysession = new AndroidDriver<AppiumWebElement>(token: _testProjectToken, appiumOptions: _appiumOptions, projectName: "Android", jobName: "Test");

The app opens on the device but immediately shows the following error in Visual Studio:

Object reference not set to an instance of an object.System.NullReferenceException: Object reference not set to an instance of an object.
   at TestProject.OpenSDK.Internal.CallStackAnalysis.SpecFlowAnalyzer.IsSpecFlow(MethodBase method)
   at TestProject.OpenSDK.Internal.CallStackAnalysis.StackTraceHelper.<>c.<TryDetectSpecFlow>b__10_1(MethodBase m)
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source, Func2 predicate)
   at TestProject.OpenSDK.Internal.CallStackAnalysis.StackTraceHelper.TryDetectSpecFlow()
   at TestProject.OpenSDK.Drivers.Android.AndroidDriver1..ctor(Uri remoteAddress, String token, AppiumOptions appiumOptions, String projectName, String jobName, Boolean disableReports, ReportType reportType, String reportName, String reportPath, Nullable'1 remoteConnectionTimeout, Int32 restClientTimeout)
   at SpecFlowProject_Two.Steps.CalculatorStepDefinitions.BeforeScenario() in \Steps\CalculatorStepDefinitions.cs:line 35
   at lambda_method(Closure , IContextManager )
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireEvents(HookType hookType)
   at TechTalk.SpecRun.SpecFlowPlugin.Runtime.RunnerTestExecutionEngine.FireScenarioEvents(HookType bindingEvent)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnScenarioStart()
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnAfterLastStep()
   at TechTalk.SpecRun.SpecFlowPlugin.Runtime.RunnerTestExecutionEngine.OnAfterLastStep()
   at TechTalk.SpecFlow.TestRunner.CollectScenarioErrors()
   at SpecFlowProject_Two.Features.CalculatorFeature.ScenarioCleanup()
   at SpecFlowProject_Two.Features.CalculatorFeature.AddTwoNumbers() in \Features\Calculator.feature:line 8
   at TechTalk.SpecRun.Framework.TaskExecutors.StaticOrInstanceMethodExecutor.ExecuteInternal(ITestThreadExecutionContext testThreadExecutionContext)
   at TechTalk.SpecRun.Framework.TaskExecutors.StaticOrInstanceMethodExecutor.Execute(ITestThreadExecutionContext testThreadExecutionContext)
   at TechTalk.SpecRun.Framework.TestNodeTask.Execute()

The driver create code works perfectly ok if I just use TestProject without SpecFlow. I also successfully use very similar code to create tests on SpecFlow/BrowserStack projects.

Not sure if it is something I’m doing/not doing or if it is a problem in TestProject or SpecFlow (or a combination).
Any assistance would be appreciated!

What I have tried so far:

  • cleared out TestProject and SpecFlow NuGet packages and reinstalled
  • used different configurations of TestProject and SpecFlow NuGet packages
  • deleted the bin and obj folders and rebuilt the solution
  • placed the code in one file
  • used a different app

I have cut the code down to use the SpecFlow Calculator demo but still get the NullReferenceException error.

Feature File

Feature: Calculator

Scenario: Add two numbers
	Given the first number is 50
	And the second number is 70
	When the two numbers are added
	Then the result should be 120

Steps Code

using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Enums;
using TechTalk.SpecFlow;
using TestProject.OpenSDK.Drivers.Android;

namespace SpecFlowProject_Two.Steps
{
    [Binding]
    public sealed class CalculatorStepDefinitions
    {
        private readonly ScenarioContext _scenarioContext;

        public AndroidDriver<AppiumWebElement> mysession;
        public const string _androidDeviceName = "motorola-moto_g__5_-ZY3224S6TZ";
        public const string _androidAppActivity = "com.android.calculator2.Calculator";
        public const string _appPackage = "com.google.android.calculator";
        public const string _testProjectToken = "token";

        [BeforeScenario]
        public void BeforeScenario()
        {
            if (mysession == null)
            {
                AppiumOptions _appiumOptions = new AppiumOptions();

                _appiumOptions.AddAdditionalCapability(MobileCapabilityType.PlatformName, MobilePlatform.Android);
                _appiumOptions.AddAdditionalCapability(MobileCapabilityType.PlatformVersion, "8.1.0");
                _appiumOptions.AddAdditionalCapability(MobileCapabilityType.DeviceName, _androidDeviceName);

                _appiumOptions.AddAdditionalCapability(AndroidMobileCapabilityType.AppPackage, _appPackage);
                _appiumOptions.AddAdditionalCapability(AndroidMobileCapabilityType.AppActivity, _androidAppActivity);

                mysession = new AndroidDriver<AppiumWebElement>(token: _testProjectToken, appiumOptions: _appiumOptions, projectName: "Android", jobName: "Test");

                mysession.StartActivity(appPackage: _appPackage, appActivity: _androidAppActivity);
            }
        }

        public CalculatorStepDefinitions(ScenarioContext scenarioContext)
        {
            _scenarioContext = scenarioContext;
        }

        [Given("the first number is (.*)")]
        public void GivenTheFirstNumberIs(int number)
        {
            _scenarioContext.Pending();
        }

        [Given("the second number is (.*)")]
        public void GivenTheSecondNumberIs(int number)
        {
            _scenarioContext.Pending();
        }

        [When("the two numbers are added")]
        public void WhenTheTwoNumbersAreAdded()
        {
            _scenarioContext.Pending();
        }

        [Then("the result should be (.*)")]
        public void ThenTheResultShouldBe(int result)
        {
            _scenarioContext.Pending();
        }

        [AfterScenario]
        public void AfterScenario()
        {
            if (mysession != null)
            {
                mysession.Quit();
                mysession = null;
            }
        }
    }
}

Project File

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
    <PackageReference Include="SpecFlow.Plus.LivingDocPlugin" Version="3.9.57" />
    <PackageReference Include="SpecRun.SpecFlow" Version="3.9.31" />
    <PackageReference Include="FluentAssertions" Version="6.7.0" />
    <PackageReference Include="TestProject.OpenSDK" Version="1.2.4" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Drivers\" />
    <Folder Include="Hooks\" />
  </ItemGroup>

</Project>

Never mind. Installed TestProject.OpenSDK.SpecFlowPlugin NuGet package again and is now working! Weird! :thinking:

Spoke too soon!
It only works if the driver “mysession” is created in the constructor.
Still getting the null reference exception if the driver is attempted in BeforeScenario.

An Object is an instance of a Class , it is stored some where in memory. A reference is what is used to describe the pointer to the memory location where the Object resides. The message “object reference not set to an instance of an object” means that you are referring to an object the does not exist or was deleted or cleaned up. It’s usually better to avoid a NullReferenceException than to handle it after it occurs. To prevent the error, objects that could be null should be tested for null before being used.

if (mClass != null)
{
  // Go ahead and use mClass
  mClass.property = ...
}
else
{
  // Attempting to use mClass here will result in NullReferenceException
}

A NullReferenceException typically reflects developer error and is thrown in the following scenarios:

  • Forgotten to instantiate a reference type.
  • Forgotten to dimension an array before initializing it.
  • Is thrown by a method that is passed null.
  • Get a null return value from a method, then call a method on the returned type.
  • Using an expression to retrieve a value and, although checking whether the value is null.
  • Enumerating the elements of an array that contains reference types, and attempt to process one of the elements.