Not a Duplicate
I know how to handle NullRefrenceException. Please read the entire question before flagging it as duplicate. I want to know which element in my code is throwing the exception.
I ran into a problem while trying to read command line arguments/StartupEventsArgs in App.xaml.cs, and couldn't figure out what's wrong with my code. The same code works in another project, but doesn't work in my current project.
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
if (AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData != null
&& AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Length > 0)
{
string fname = "Something";
try
{
fname = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0];
Uri uri = new Uri(fname);
fname = uri.LocalPath;
this.Properties["tempPath"] = fname;
}
catch (Exception ex)
{
//Exception Handling
}
}
base.OnStartup(e);
}
}
Edited Code
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
if (AppDomain.CurrentDomain.SetupInformation.ActivationArguments != null
&& AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Length > 0)
{
string fname = "Something";
try
{
fname = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0];
Uri uri = new Uri(fname);
fname = uri.LocalPath;
this.Properties["tempPath"] = fname;
}
catch (Exception ex)
{
//Exception Handling
}
}
base.OnStartup(e);
}
}
Whenever I try to start the project I am getting a NullRefernceException. I am including a screenshot.
Can someone tell me what's wrong?