0

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.

enter image description here

Can someone tell me what's wrong?

tinku92
  • 109
  • 7
  • 1
    Hey tinku, In your post you mentioned reading command line args. Which can be accessed from StartupEventArgs e, e.Args In most cases, I believe that if ActivationData is null, it may be because ActivationArguments are null. Perhaps checking null on that instead might help you. I'm not at a pc with VS on right now however so can't poke around myself; and it's been a while since I had to grab such data. – Gavin Lanata Jul 03 '15 at 07:30
  • Just had a play around with a test WPF application. Since it's been a while since I've done this myself I believe things have changed in newer versions of .Net. If it was a file path from an association you where after. I was able to retrieve the file path from this. Of course in my test project I'm not testing to see if the args are null. this.Properties["tempPath"] = e.Args[0]; – Gavin Lanata Jul 03 '15 at 07:51
  • Gavin, It worked thank you. I think they have changed it in newer versions of .net, but I am not sure. All the solutions I checked got he path of file from AppDomain...ActivationData[0]. Thank you once again. – tinku92 Jul 03 '15 at 07:56
  • You're very welcome. – Gavin Lanata Jul 03 '15 at 08:00
  • it worked the first time, but after building it second time it ran into the same problem again. – tinku92 Jul 03 '15 at 08:05
  • If you can, can you edit your code in your question to show your current code? – Gavin Lanata Jul 03 '15 at 08:17
  • I added the code as you requested. – tinku92 Jul 03 '15 at 08:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82268/discussion-between-gavin-lanata-and-tinku92). – Gavin Lanata Jul 03 '15 at 08:22

0 Answers0