System.NullReferenceException occurred in

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andreas Wirén

    System.NullReferenceException occurred in

    Hi I'm a C# .NET newbie doing a minor school project but I'm getting a
    strange error message.

    'System.NullRef erenceException ' occurred in system.windows. forms.dll
    Additional information: Object reference not set to an instance of an
    object."

    As my references look I seem to be using the old framework(1.0.3 705) for
    this, but re-referencing to the newer framework(1.1.4 322) dll's produces the
    same error. This happens under all circumstances when I'm trying to compile
    a fully managed project. Even from scratch when trying to just compile the
    empty form this happens.

    If you read the old thread I've included you can see that he changed his
    sourcepath for the whole project to something that is not put in the
    MyDocuments folders. I did exactly the same thing and it worked, the
    question from me is WHY? All other apps (so far) compiles flawless, it's
    only these C# Windows Applications Projects that fail.

    Regards,

    Andreas Wirén

    OS : Windows XP Professional SP1 with all the recent hotfixes and updates.

    using System;
    using System.Drawing;
    using System.Collecti ons;
    using System.Componen tModel;
    using System.Windows. Forms;
    using System.Data;

    namespace WindowsApplicat ion1
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows. Forms.Form
    {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.Componen tModel.Containe r components = null;

    public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeCompo nent();

    //
    // TODO: Add any constructor code after InitializeCompo nent call
    //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Disp ose();
    }
    }
    base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeCompo nent()
    {
    //
    // Form1
    //
    this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
    this.ClientSize = new System.Drawing. Size(292, 266);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHan dler(this.Form1 _Load);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.Run (new Form1());
    }

    private void Form1_Load(obje ct sender, System.EventArg s e)
    {

    }
    }
    }


    tion.Run(new%2B Form1())%253B%2 522%2B%2522obje ct%2Breference% 2Bnot%2Bset%252 2
    %26hl%3Dsv%26lr %3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D6FAD a.54216%2524g33 .74
    9173%2540weber. videotron.net%2 6rnum%3D6


  • Dmitry Karneyev

    #2
    Re: System.NullRefe renceException occurred in

    Andreas,
    You may use non created object.
    for example

    MyClass myobject;
    myobject.MyMeth od();
    -- error;

    You shoul do:

    MyClass myobject;
    myobject = new MyClass();
    myobject.MyMeth od();

    "Andreas Wirén" <andreas@clan.d cs.se> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ:
    news:HBPlb.3459 5$dP1.110430@ne wsc.telia.net.. .[color=blue]
    > Hi I'm a C# .NET newbie doing a minor school project but I'm getting a
    > strange error message.
    >
    > 'System.NullRef erenceException ' occurred in system.windows. forms.dll
    > Additional information: Object reference not set to an instance of an
    > object."
    >
    > As my references look I seem to be using the old framework(1.0.3 705) for
    > this, but re-referencing to the newer framework(1.1.4 322) dll's produces[/color]
    the[color=blue]
    > same error. This happens under all circumstances when I'm trying to[/color]
    compile[color=blue]
    > a fully managed project. Even from scratch when trying to just compile the
    > empty form this happens.
    >
    > If you read the old thread I've included you can see that he changed his
    > sourcepath for the whole project to something that is not put in the
    > MyDocuments folders. I did exactly the same thing and it worked, the
    > question from me is WHY? All other apps (so far) compiles flawless, it's
    > only these C# Windows Applications Projects that fail.
    >
    > Regards,
    >
    > Andreas Wirén
    >
    > OS : Windows XP Professional SP1 with all the recent hotfixes and updates.
    >
    > using System;
    > using System.Drawing;
    > using System.Collecti ons;
    > using System.Componen tModel;
    > using System.Windows. Forms;
    > using System.Data;
    >
    > namespace WindowsApplicat ion1
    > {
    > /// <summary>
    > /// Summary description for Form1.
    > /// </summary>
    > public class Form1 : System.Windows. Forms.Form
    > {
    > /// <summary>
    > /// Required designer variable.
    > /// </summary>
    > private System.Componen tModel.Containe r components = null;
    >
    > public Form1()
    > {
    > //
    > // Required for Windows Form Designer support
    > //
    > InitializeCompo nent();
    >
    > //
    > // TODO: Add any constructor code after InitializeCompo nent call
    > //
    > }
    >
    > /// <summary>
    > /// Clean up any resources being used.
    > /// </summary>
    > protected override void Dispose( bool disposing )
    > {
    > if( disposing )
    > {
    > if (components != null)
    > {
    > components.Disp ose();
    > }
    > }
    > base.Dispose( disposing );
    > }
    >
    > #region Windows Form Designer generated code
    > /// <summary>
    > /// Required method for Designer support - do not modify
    > /// the contents of this method with the code editor.
    > /// </summary>
    > private void InitializeCompo nent()
    > {
    > //
    > // Form1
    > //
    > this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
    > this.ClientSize = new System.Drawing. Size(292, 266);
    > this.Name = "Form1";
    > this.Text = "Form1";
    > this.Load += new System.EventHan dler(this.Form1 _Load);
    >
    > }
    > #endregion
    >
    > /// <summary>
    > /// The main entry point for the application.
    > /// </summary>
    > [STAThread]
    > static void Main()
    > {
    > Application.Run (new Form1());
    > }
    >
    > private void Form1_Load(obje ct sender, System.EventArg s e)
    > {
    >
    > }
    > }
    > }
    >
    >[/color]
    http://groups.google.se/groups?hl=sv...3D%2522Applica[color=blue]
    >[/color]
    tion.Run(new%2B Form1())%253B%2 522%2B%2522obje ct%2Breference% 2Bnot%2Bset%252 2[color=blue]
    >[/color]
    %26hl%3Dsv%26lr %3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D6FAD a.54216%2524g33 .74[color=blue]
    > 9173%2540weber. videotron.net%2 6rnum%3D6
    >
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: System.NullRefe renceException occurred in

      Dmitry Karneyev <karneyev@msn.c om> wrote:[color=blue]
      > Andreas,
      > You may use non created object.
      > for example
      >
      > MyClass myobject;
      > myobject.MyMeth od();
      > -- error;
      >
      > You shoul do:
      >
      > MyClass myobject;
      > myobject = new MyClass();
      > myobject.MyMeth od();[/color]

      But that's not what's happening here. The code compiles and runs fine.
      Note that according to the message, the error occurs during
      compilation, not at runtime.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Morten Wennevik

        #4
        Re: System.NullRefe renceException occurred in

        It appears to give a NullReferenceEx ception from system.windows. forms.dll

        If you use Visual Studio 2002 you can't use v1.1.

        I'd try to reinstall the framework.

        --
        Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

        Comment

        • Andreas Wirén

          #5
          Re: System.NullRefe renceException occurred in

          Hmm, I'm not sure if I know what the difference between runtime and
          compilation is. Anyway, after pressing F5 the program launches but "locks"
          and the debugger steps in and gives the error message. This is already a
          fresh installation of VS.NET. All I've done is a few(4-5) VB apps.

          And I really doubt it's an code error as someone said, the error occurs on
          this machine I'm on right now, but not on another box, running exactly the
          same VS release coupled with the exact same code I posted earlier.

          My guess is that something stupid has happened with some of the DLL files or
          some other linked files used by the compiler. The other box (that the
          compile works on) has a much fresher install of XP (with all hotfixes and
          updates) but does not have the SP1 of VS.NET installed.

          "Morten Wennevik" <MortenWennevik @hotmail.com> skrev i meddelandet
          news:oprxhz9njt hntkfz@localhos t...
          It appears to give a NullReferenceEx ception from system.windows. forms.dll

          If you use Visual Studio 2002 you can't use v1.1.

          I'd try to reinstall the framework.

          --
          Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: System.NullRefe renceException occurred in

            Andreas Wirén <andreas@clan.d cs.se> wrote:[color=blue]
            > Hmm, I'm not sure if I know what the difference between runtime and
            > compilation is. [/color]

            If the error occurs when you compile the code, it's a compile-time
            error.

            If the error occurs when you run the code, it's a run-time error.
            [color=blue]
            > Anyway, after pressing F5 the program launches but "locks"
            > and the debugger steps in and gives the error message. This is already a
            > fresh installation of VS.NET. All I've done is a few(4-5) VB apps.[/color]

            Ah - that's a run-time error. (When you say about "the compile works" I
            inferred that it was a compile-time error.)
            [color=blue]
            > And I really doubt it's an code error as someone said, the error occurs on
            > this machine I'm on right now, but not on another box, running exactly the
            > same VS release coupled with the exact same code I posted earlier.
            >
            > My guess is that something stupid has happened with some of the DLL filesor
            > some other linked files used by the compiler. The other box (that the
            > compile works on) has a much fresher install of XP (with all hotfixes and
            > updates) but does not have the SP1 of VS.NET installed.[/color]

            It doesn't sound like it's a compiler error. What happens if you
            compile it on the "working" machine and then copy the executable which
            is produced onto the "broken" machine and run it there?

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Andreas Wirén

              #7
              Re: System.NullRefe renceException occurred in

              The file works flawless when compiled and executed on the "broken" machine.
              I'm also not certain if it really goes into runtime, all that happens when I
              press F5 is that the debugger points to a line in the code:

              static void Main()
              {

              Application.Run (new Form1());

              }<-----

              and a messagebox pops up with the error message System.NullRefe rence...

              The form do appear in the taskbar though but when clicked it's only white.
              Still the only solution to this problem seem to be to move all my project
              files to some other folder on the drive, but that seems like a really silly
              solution :-)


              "Jon Skeet [C# MVP]" <skeet@pobox.co m> skrev i meddelandet
              news:MPG.1a01f4 9f5734902b98991 1@msnews.micros oft.com...
              Andreas Wirén <andreas@clan.d cs.se> wrote:[color=blue]
              > Hmm, I'm not sure if I know what the difference between runtime and
              > compilation is.[/color]

              If the error occurs when you compile the code, it's a compile-time
              error.

              If the error occurs when you run the code, it's a run-time error.
              [color=blue]
              > Anyway, after pressing F5 the program launches but "locks"
              > and the debugger steps in and gives the error message. This is already a
              > fresh installation of VS.NET. All I've done is a few(4-5) VB apps.[/color]

              Ah - that's a run-time error. (When you say about "the compile works" I
              inferred that it was a compile-time error.)
              [color=blue]
              > And I really doubt it's an code error as someone said, the error occurs on
              > this machine I'm on right now, but not on another box, running exactly the
              > same VS release coupled with the exact same code I posted earlier.
              >
              > My guess is that something stupid has happened with some of the DLL files[/color]
              or[color=blue]
              > some other linked files used by the compiler. The other box (that the
              > compile works on) has a much fresher install of XP (with all hotfixes and
              > updates) but does not have the SP1 of VS.NET installed.[/color]

              It doesn't sound like it's a compiler error. What happens if you
              compile it on the "working" machine and then copy the executable which
              is produced onto the "broken" machine and run it there?

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: System.NullRefe renceException occurred in

                Andreas Wirén <andreas@clan.d cs.se> wrote:[color=blue]
                > The file works flawless when compiled and executed on the "broken" machine.[/color]

                Eh? Do you mean when it's compiled elsewhere and then executed on the
                "broken" machine? If it works fine when compiler *and* executed on the
                "broken" machine, I can't see what's wrong at all!
                [color=blue]
                > I'm also not certain if it really goes into runtime, all that happens when I
                > press F5 is that the debugger points to a line in the code:
                >
                > static void Main()
                > {
                >
                > Application.Run (new Form1());
                >
                > }<-----
                >
                > and a messagebox pops up with the error message System.NullRefe rence...[/color]

                That's runtime. If it only breaks when you run it, it's runtime.
                [color=blue]
                > The form do appear in the taskbar though but when clicked it's only white..
                > Still the only solution to this problem seem to be to move all my project
                > files to some other folder on the drive, but that seems like a really silly
                > solution :-)[/color]

                Okay, I'm very confused as to what's provoking this now. Could you give
                a brief summary of when it works and when it doesn't, on which machine,
                etc?

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Andreas Wirén

                  #9
                  Re: System.NullRefe renceException occurred in

                  Sorry if I was confusing you :-)

                  Machine1 (not the broken one) : Compile - OK! , Executing - OK!
                  Machine2 (the broken one) : Compile - Error!, Executing - Error!

                  Machine2 (the broken one) while using the binary from Machine1: Executing -
                  OK!

                  Hope that clear things out.

                  Thanks for your time btw! It's not really that big issue but it's annoying
                  not being able to save documents/projects to whereever you want.
                  "Jon Skeet [C# MVP]" <skeet@pobox.co m> skrev i meddelandet
                  news:MPG.1a01fb 02c6fa655898991 5@msnews.micros oft.com...
                  Andreas Wirén <andreas@clan.d cs.se> wrote:[color=blue]
                  > The file works flawless when compiled and executed on the "broken"[/color]
                  machine.

                  Eh? Do you mean when it's compiled elsewhere and then executed on the
                  "broken" machine? If it works fine when compiler *and* executed on the
                  "broken" machine, I can't see what's wrong at all!
                  [color=blue]
                  > I'm also not certain if it really goes into runtime, all that happens when[/color]
                  I[color=blue]
                  > press F5 is that the debugger points to a line in the code:
                  >
                  > static void Main()
                  > {
                  >
                  > Application.Run (new Form1());
                  >
                  > }<-----
                  >
                  > and a messagebox pops up with the error message System.NullRefe rence...[/color]

                  That's runtime. If it only breaks when you run it, it's runtime.
                  [color=blue]
                  > The form do appear in the taskbar though but when clicked it's only white.
                  > Still the only solution to this problem seem to be to move all my project
                  > files to some other folder on the drive, but that seems like a really[/color]
                  silly[color=blue]
                  > solution :-)[/color]

                  Okay, I'm very confused as to what's provoking this now. Could you give
                  a brief summary of when it works and when it doesn't, on which machine,
                  etc?

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too


                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: System.NullRefe renceException occurred in

                    Andreas Wirén <andreas@clan.d cs.se> wrote:[color=blue]
                    > Sorry if I was confusing you :-)
                    >
                    > Machine1 (not the broken one) : Compile - OK! , Executing - OK!
                    > Machine2 (the broken one) : Compile - Error!, Executing - Error![/color]

                    When you say "Compile - Error!" are you now saying that you *do* get an
                    error at compile-time?
                    [color=blue]
                    > Thanks for your time btw! It's not really that big issue but it's annoying
                    > not being able to save documents/projects to whereever you want.[/color]

                    It's also *only* when you save the project in My Documents somewhere
                    that you get the problem, yes?

                    It does sound highly bizarre to me.

                    Out of interest, what happens when you have the project in My Documents
                    but compile the source code from the command line?

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    • Andreas Wirén

                      #11
                      Re: System.NullRefe renceException occurred in

                      With "Compile - Error!" I mean it's not giving me an error-free compile. The
                      ..exe file is still created and I suppose that means it's an run-time error.
                      However, I found something very funny, if I just move the file (the "not
                      error free exe") down one folder in the hierarchy (using Explorer) the file
                      executes flawless.

                      Files on Machine1:

                      c:\documents and settings\Andrea s.ATHLON1\My Documents\Visua l Studio
                      Projects\Window sApplication3\b in\Debug\test1. exe
                      c:\documents and settings\Andrea s.ATHLON1\My Documents\Visua l Studio
                      Projects\Window sApplication3\b in\Debug\test2. exe

                      test1.exe is compiled on Machine1 without any errors, test2 is compiled on
                      Machine2 but with errors.
                      test1.exe can be executed anywhere on Machine1 but it gives me an exception
                      on Machine2 when executed from the above specific dir.
                      test2.exe works in the exact same mysterious ways as test1.exe but is
                      compiled with errors as I said earlier.

                      But if I just move any of the two exe files to another dir, say
                      c:\documents and settings\Andrea s.ATHLON1\My Documents\Visua l Studio
                      Projects\Window sApplication3\b in\test1.exe

                      Then the file executes without a hitch :-P

                      Haha, this sure is a mess but I hope you understand.

                      "Jon Skeet [C# MVP]" <skeet@pobox.co m> skrev i meddelandet
                      news:MPG.1a020a e45bec44998991a @msnews.microso ft.com...
                      Andreas Wirén <andreas@clan.d cs.se> wrote:[color=blue]
                      > Sorry if I was confusing you :-)
                      >
                      > Machine1 (not the broken one) : Compile - OK! , Executing - OK!
                      > Machine2 (the broken one) : Compile - Error!, Executing - Error![/color]

                      When you say "Compile - Error!" are you now saying that you *do* get an
                      error at compile-time?
                      [color=blue]
                      > Thanks for your time btw! It's not really that big issue but it's annoying
                      > not being able to save documents/projects to whereever you want.[/color]

                      It's also *only* when you save the project in My Documents somewhere
                      that you get the problem, yes?

                      It does sound highly bizarre to me.

                      Out of interest, what happens when you have the project in My Documents
                      but compile the source code from the command line?

                      --
                      Jon Skeet - <skeet@pobox.co m>
                      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                      If replying to the group, please do not mail me too


                      Comment

                      • Jon Skeet [C# MVP]

                        #12
                        Re: System.NullRefe renceException occurred in

                        Andreas Wirén <andreas@clan.d cs.se> wrote:[color=blue]
                        > With "Compile - Error!" I mean it's not giving me an error-free compile. [/color]

                        So it's giving you an error at compile-time? What error is that?
                        [color=blue]
                        > The .exe file is still created and I suppose that means it's an run-time error.
                        > However, I found something very funny, if I just move the file (the "not
                        > error free exe") down one folder in the hierarchy (using Explorer) the file
                        > executes flawless.[/color]

                        Okay, that's very strange - I'm entirely baffled now, I'm afraid :(

                        --
                        Jon Skeet - <skeet@pobox.co m>
                        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                        If replying to the group, please do not mail me too

                        Comment

                        Working...