NullReferenceException

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

    #1

    NullReferenceException

    could anybody describes a scenario where this error will occur?
    i have visited the page about the NullReferenceEx ception in msdn library but
    i still have no idea what it is about.
    thanks.

    System.NullRefe renceException: Object reference not set to an instance of an
    object.

    --
    Xero


    My personal web portal
  • Herfried K. Wagner [MVP]

    #2
    Re: NullReferenceEx ception

    "Xero" <jeff_at_chezje ff_dot_net> schrieb:[color=blue]
    > could anybody describes a scenario where this error will occur?
    > i have visited the page about the NullReferenceEx ception in msdn library
    > but
    > i still have no idea what it is about.
    > thanks.
    >
    > System.NullRefe renceException: Object reference not set to an instance of
    > an
    > object.[/color]


    \\\
    Dim f As Foo
    f.Bla()
    ///

    The 'NullReferenceE xception' will occur on the second line if 'Bla' is an
    instance member of 'Foo' because the variable 'f' doesn't point to an
    instance of 'Foo'. Instead, this variable points to 'Nothing' ("null
    reference") and the method cannot be called.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Supra

      #3
      Re: NullReferenceEx ception

      u can use try....end try

      Herfried K. Wagner [MVP] wrote:
      [color=blue]
      > "Xero" <jeff_at_chezje ff_dot_net> schrieb:
      >[color=green]
      >> could anybody describes a scenario where this error will occur?
      >> i have visited the page about the NullReferenceEx ception in msdn
      >> library but
      >> i still have no idea what it is about.
      >> thanks.
      >>
      >> System.NullRefe renceException: Object reference not set to an
      >> instance of an
      >> object.[/color]
      >
      >
      >
      > \\\
      > Dim f As Foo
      > f.Bla()
      > ///
      >
      > The 'NullReferenceE xception' will occur on the second line if 'Bla' is
      > an instance member of 'Foo' because the variable 'f' doesn't point to
      > an instance of 'Foo'. Instead, this variable points to 'Nothing'
      > ("null reference") and the method cannot be called.
      >[/color]

      Comment

      • Tom Spink

        #4
        Re: NullReferenceEx ception

        Supra wrote:[color=blue]
        > u can use try....end try[/color]

        To catch the exception, yes. I believe the original poster asked for a
        situation when NullReferenceEx ception might occur.

        -- Tom Spink
        [color=blue]
        >
        > Herfried K. Wagner [MVP] wrote:
        >[color=green]
        >> "Xero" <jeff_at_chezje ff_dot_net> schrieb:
        >>[color=darkred]
        >>> could anybody describes a scenario where this error will occur?
        >>> i have visited the page about the NullReferenceEx ception in msdn
        >>> library but
        >>> i still have no idea what it is about.
        >>> thanks.
        >>>
        >>> System.NullRefe renceException: Object reference not set to an
        >>> instance of an
        >>> object.[/color]
        >>
        >>
        >>
        >> \\\
        >> Dim f As Foo
        >> f.Bla()
        >> ///
        >>
        >> The 'NullReferenceE xception' will occur on the second line if 'Bla' is
        >> an instance member of 'Foo' because the variable 'f' doesn't point to
        >> an instance of 'Foo'. Instead, this variable points to 'Nothing'
        >> ("null reference") and the method cannot be called.
        >>[/color][/color]

        Comment

        • Xero

          #5
          Re: NullReferenceEx ception

          hi. thanks for your reply.
          how about this code:

          base.Test.NumOf Qs = 10
          For m = 1 to base.Test.NumOf Qs
          base.UserAns.an swered(m) = False
          Next

          'base' is a form in the solution.
          UserAns is a structure located in 'base' and answered() is a member of
          UserAns.
          base.Test.NumOf Qs and base.UserAns.an swered() are Public Shared.
          VS compiler showed that the NullReferenceEx ception occured at line 3.
          why is this happening?

          thanks again.

          --
          Xero


          My personal web portal


          "Herfried K. Wagner [MVP]" wrote:
          [color=blue]
          > "Xero" <jeff_at_chezje ff_dot_net> schrieb:[color=green]
          > > could anybody describes a scenario where this error will occur?
          > > i have visited the page about the NullReferenceEx ception in msdn library
          > > but
          > > i still have no idea what it is about.
          > > thanks.
          > >
          > > System.NullRefe renceException: Object reference not set to an instance of
          > > an
          > > object.[/color]
          >
          >
          > \\\
          > Dim f As Foo
          > f.Bla()
          > ///
          >
          > The 'NullReferenceE xception' will occur on the second line if 'Bla' is an
          > instance member of 'Foo' because the variable 'f' doesn't point to an
          > instance of 'Foo'. Instead, this variable points to 'Nothing' ("null
          > reference") and the method cannot be called.
          >
          > --
          > M S Herfried K. Wagner
          > M V P <URL:http://dotnet.mvps.org/>
          > V B <URL:http://classicvb.org/petition/>
          >
          >[/color]

          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: NullReferenceEx ception

            "Xero" <jeff_at_chezje ff_dot_net> schrieb:[color=blue]
            > base.Test.NumOf Qs = 10
            > For m = 1 to base.Test.NumOf Qs[/color]

            Try 'For m = 0 To Base.Test.NumOf Qs - 1'.
            [color=blue]
            > base.UserAns.an swered(m) = False
            > Next
            >
            > 'base' is a form in the solution.
            > UserAns is a structure located in 'base' and answered() is a member of
            > UserAns.
            > base.Test.NumOf Qs and base.UserAns.an swered() are Public Shared.
            > VS compiler showed that the NullReferenceEx ception occured at line 3.
            > why is this happening?[/color]

            I assume that it's a runtime error and not a compile-time error.

            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://classicvb.org/petition/>

            Comment

            • Xero

              #7
              Re: NullReferenceEx ception

              it doesn't work ...
              any other ideas?

              --
              Xero


              My personal web portal


              "Herfried K. Wagner [MVP]" wrote:
              [color=blue]
              > "Xero" <jeff_at_chezje ff_dot_net> schrieb:[color=green]
              > > base.Test.NumOf Qs = 10
              > > For m = 1 to base.Test.NumOf Qs[/color]
              >
              > Try 'For m = 0 To Base.Test.NumOf Qs - 1'.
              >[color=green]
              > > base.UserAns.an swered(m) = False
              > > Next
              > >
              > > 'base' is a form in the solution.
              > > UserAns is a structure located in 'base' and answered() is a member of
              > > UserAns.
              > > base.Test.NumOf Qs and base.UserAns.an swered() are Public Shared.
              > > VS compiler showed that the NullReferenceEx ception occured at line 3.
              > > why is this happening?[/color]
              >
              > I assume that it's a runtime error and not a compile-time error.
              >
              > --
              > M S Herfried K. Wagner
              > M V P <URL:http://dotnet.mvps.org/>
              > V B <URL:http://classicvb.org/petition/>
              >[/color]

              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: NullReferenceEx ception

                "Xero" <jeff_at_chezje ff_dot_net> schrieb:[color=blue]
                > it doesn't work ...
                > any other ideas?[/color]

                Check the content of the array or indexed property 'answered' in the
                debugger.

                --
                M S Herfried K. Wagner
                M V P <URL:http://dotnet.mvps.org/>
                V B <URL:http://classicvb.org/petition/>

                Comment

                • Xero

                  #9
                  Re: NullReferenceEx ception

                  hi.
                  i have solved the problem.
                  it turned out that i forgot to ReDim 'answered' before the loop and the
                  length of the 'answered' array was zero.
                  it is now fixed.

                  but why isn't the 'Index is out of array bound' exception thrown instead?

                  thanks again!

                  --
                  Xero


                  My personal web portal


                  "Herfried K. Wagner [MVP]" wrote:
                  [color=blue]
                  > "Xero" <jeff_at_chezje ff_dot_net> schrieb:[color=green]
                  > > it doesn't work ...
                  > > any other ideas?[/color]
                  >
                  > Check the content of the array or indexed property 'answered' in the
                  > debugger.
                  >
                  > --
                  > M S Herfried K. Wagner
                  > M V P <URL:http://dotnet.mvps.org/>
                  > V B <URL:http://classicvb.org/petition/>
                  >
                  >[/color]

                  Comment

                  • Supra

                    #10
                    Re: NullReferenceEx ception

                    u should read microsoft press book :-[

                    Xero wrote:
                    [color=blue]
                    >hi.
                    >i have solved the problem.
                    >it turned out that i forgot to ReDim 'answered' before the loop and the
                    >length of the 'answered' array was zero.
                    >it is now fixed.
                    >
                    >but why isn't the 'Index is out of array bound' exception thrown instead?
                    >
                    >thanks again!
                    >
                    >
                    >[/color]

                    Comment

                    Working...