Initializing list in constructor ObjectNullReferenceException

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • VonGuller

    Initializing list in constructor ObjectNullReferenceException

    Hello,

    I have a code like this(as an example) :

    public class class1:UserCont rol
    {
    private MyOwnCollection BasedClass pList1;

    public MyOwnCollection BasedClass List1
    {
    get{ return pList1;}
    set{pList1 = value;}
    }

    public class1()
    {
    pList1 = new MyOwnCollection BasedClass()
    }

    private void class1_load(obj ect sender, EventArgs e)
    {
    LoadMethod();
    }

    private void LoadMethod()
    {
    int count = List1.Count;
    }

    }

    This is a user control on a form docked as fill.

    In Most of the computers this works ok, but on some computers this
    crashes on LoadMethod(). For somereason the List1 is null and there
    comes ObjectNullRefer enceException:O bject reference not set to an
    instance of an object.

    Why is List1 null? It is initialized in the constructor...T he
    MyOwnCollection BasedClass constructor doesn't have any code...so it
    should happen fast.

    Regards, Jyri

  • Marc Gravell

    #2
    Re: Initializing list in constructor ObjectNullRefer enceException

    The speed of the ctor shouldn't be an issue anyway...

    Are you *sure* that there is no code (possibly later in the ctor of your
    actual class, possible between "new" and "Show") that might fire through
    some 'orrible maze of code and end up setting pList1 to null?

    IMO, since you inner code relies on List1 not being null, and you have an
    unckecke setter, you should (one of):
    * Change all code to anticipace possible nulls, and handle gracefully
    (usually by a short-circuit)
    * Change the setter to throw an ArgumentNullExc eption if value == null

    Actually, the latter, along with the call start on a break-point, might help
    you track the offender...

    Marc


    Comment

    • Marc Gravell

      #3
      Re: Initializing list in constructor ObjectNullRefer enceException

      Sorry: caffeine shakes!

      unckecke -unchecked
      call start -call stack

      Marc


      Comment

      • Michael Nemtsev

        #4
        RE: Initializing list in constructor ObjectNullRefer enceException

        You pList1 initialization works wrong in some cases, apparently. Just con
        what can be the reason into MyOwnCollection BasedClass
        Insert validation, before using pList1 that your pList1 was initialized
        successfully

        --
        WBR,
        Michael Nemtsev :: blog: http://spaces.live.com/laflour

        "At times one remains faithful to a cause only because its opponents do not
        cease to be insipid." (c) Friedrich Nietzsche




        "VonGuller" wrote:
        Hello,
        >
        I have a code like this(as an example) :
        >
        public class class1:UserCont rol
        {
        private MyOwnCollection BasedClass pList1;
        >
        public MyOwnCollection BasedClass List1
        {
        get{ return pList1;}
        set{pList1 = value;}
        }
        >
        public class1()
        {
        pList1 = new MyOwnCollection BasedClass()
        }
        >
        private void class1_load(obj ect sender, EventArgs e)
        {
        LoadMethod();
        }
        >
        private void LoadMethod()
        {
        int count = List1.Count;
        }
        >
        }
        >
        This is a user control on a form docked as fill.
        >
        In Most of the computers this works ok, but on some computers this
        crashes on LoadMethod(). For somereason the List1 is null and there
        comes ObjectNullRefer enceException:O bject reference not set to an
        instance of an object.
        >
        Why is List1 null? It is initialized in the constructor...T he
        MyOwnCollection BasedClass constructor doesn't have any code...so it
        should happen fast.
        >
        Regards, Jyri
        >
        >

        Comment

        • Michael Nemtsev

          #5
          RE: Initializing list in constructor ObjectNullRefer enceException

          Missed that MyOwnCollection BasedClass doesnt have any code in constructor.
          What's the MyOwnCollection BasedClass at all?

          --
          WBR,
          Michael Nemtsev :: blog: http://spaces.live.com/laflour

          "At times one remains faithful to a cause only because its opponents do not
          cease to be insipid." (c) Friedrich Nietzsche




          "VonGuller" wrote:
          Hello,
          >
          I have a code like this(as an example) :
          >
          public class class1:UserCont rol
          {
          private MyOwnCollection BasedClass pList1;
          >
          public MyOwnCollection BasedClass List1
          {
          get{ return pList1;}
          set{pList1 = value;}
          }
          >
          public class1()
          {
          pList1 = new MyOwnCollection BasedClass()
          }
          >
          private void class1_load(obj ect sender, EventArgs e)
          {
          LoadMethod();
          }
          >
          private void LoadMethod()
          {
          int count = List1.Count;
          }
          >
          }
          >
          This is a user control on a form docked as fill.
          >
          In Most of the computers this works ok, but on some computers this
          crashes on LoadMethod(). For somereason the List1 is null and there
          comes ObjectNullRefer enceException:O bject reference not set to an
          instance of an object.
          >
          Why is List1 null? It is initialized in the constructor...T he
          MyOwnCollection BasedClass constructor doesn't have any code...so it
          should happen fast.
          >
          Regards, Jyri
          >
          >

          Comment

          • VonGuller

            #6
            Re: Initializing list in constructor ObjectNullRefer enceException

            MyOwnCollection BasedClass is a CollectionBase inherited list that holds
            certain type of objects. You could say it's typed ArrayList.

            Michael Nemtsev wrote:
            Missed that MyOwnCollection BasedClass doesnt have any code in constructor.
            What's the MyOwnCollection BasedClass at all?
            >
            --
            WBR,
            Michael Nemtsev :: blog: http://spaces.live.com/laflour
            >
            "At times one remains faithful to a cause only because its opponents do not
            cease to be insipid." (c) Friedrich Nietzsche
            >
            >
            >
            >
            "VonGuller" wrote:
            >
            Hello,

            I have a code like this(as an example) :

            public class class1:UserCont rol
            {
            private MyOwnCollection BasedClass pList1;

            public MyOwnCollection BasedClass List1
            {
            get{ return pList1;}
            set{pList1 = value;}
            }

            public class1()
            {
            pList1 = new MyOwnCollection BasedClass()
            }

            private void class1_load(obj ect sender, EventArgs e)
            {
            LoadMethod();
            }

            private void LoadMethod()
            {
            int count = List1.Count;
            }

            }

            This is a user control on a form docked as fill.

            In Most of the computers this works ok, but on some computers this
            crashes on LoadMethod(). For somereason the List1 is null and there
            comes ObjectNullRefer enceException:O bject reference not set to an
            instance of an object.

            Why is List1 null? It is initialized in the constructor...T he
            MyOwnCollection BasedClass constructor doesn't have any code...so it
            should happen fast.

            Regards, Jyri

            Comment

            • Michael Nemtsev

              #7
              Re: Initializing list in constructor ObjectNullRefer enceException

              Hmm,does this behaviour have place in debug or release mode?

              It's hard to say what's wrong exactly, because from that perspective there
              are no errors

              Just add error handles to be sure that everything ok.
              And as Mark said in case of exception con your call stack.

              Another solution to find out what's wrong is to use SOS, to see the
              behaviour of your class - does it really into root and was not GCed
              --
              WBR,
              Michael Nemtsev :: blog: http://spaces.live.com/laflour

              "At times one remains faithful to a cause only because its opponents do not
              cease to be insipid." (c) Friedrich Nietzsche




              "VonGuller" wrote:
              MyOwnCollection BasedClass is a CollectionBase inherited list that holds
              certain type of objects. You could say it's typed ArrayList.
              >
              Michael Nemtsev wrote:
              Missed that MyOwnCollection BasedClass doesnt have any code in constructor.
              What's the MyOwnCollection BasedClass at all?

              --
              WBR,
              Michael Nemtsev :: blog: http://spaces.live.com/laflour

              "At times one remains faithful to a cause only because its opponents do not
              cease to be insipid." (c) Friedrich Nietzsche




              "VonGuller" wrote:
              Hello,
              >
              I have a code like this(as an example) :
              >
              public class class1:UserCont rol
              {
              private MyOwnCollection BasedClass pList1;
              >
              public MyOwnCollection BasedClass List1
              {
              get{ return pList1;}
              set{pList1 = value;}
              }
              >
              public class1()
              {
              pList1 = new MyOwnCollection BasedClass()
              }
              >
              private void class1_load(obj ect sender, EventArgs e)
              {
              LoadMethod();
              }
              >
              private void LoadMethod()
              {
              int count = List1.Count;
              }
              >
              }
              >
              This is a user control on a form docked as fill.
              >
              In Most of the computers this works ok, but on some computers this
              crashes on LoadMethod(). For somereason the List1 is null and there
              comes ObjectNullRefer enceException:O bject reference not set to an
              instance of an object.
              >
              Why is List1 null? It is initialized in the constructor...T he
              MyOwnCollection BasedClass constructor doesn't have any code...so it
              should happen fast.
              >
              Regards, Jyri
              >
              >
              >
              >

              Comment

              Working...