NullReferenceException

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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

System.NullReferenceException: Object reference not set to an instance of an
object.
 
Xero said:
could anybody describes a scenario where this error will occur?
i have visited the page about the NullReferenceException in msdn library
but
i still have no idea what it is about.
thanks.

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


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

The 'NullReferenceException' 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.
 
u can use try....end try
\\\
Dim f As Foo
f.Bla()
///

The 'NullReferenceException' 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.
 
Supra said:
u can use try....end try

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

-- Tom Spink
 
hi. thanks for your reply.
how about this code:

base.Test.NumOfQs = 10
For m = 1 to base.Test.NumOfQs
base.UserAns.answered(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.NumOfQs and base.UserAns.answered() are Public Shared.
VS compiler showed that the NullReferenceException occured at line 3.
why is this happening?

thanks again.

--
Xero

http://www.chezjeff.net
My personal web portal
 
Xero said:
base.Test.NumOfQs = 10
For m = 1 to base.Test.NumOfQs

Try 'For m = 0 To Base.Test.NumOfQs - 1'.
base.UserAns.answered(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.NumOfQs and base.UserAns.answered() are Public Shared.
VS compiler showed that the NullReferenceException occured at line 3.
why is this happening?

I assume that it's a runtime error and not a compile-time error.
 
Xero said:
it doesn't work ...
any other ideas?

Check the content of the array or indexed property 'answered' in the
debugger.
 
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

http://www.chezjeff.net
My personal web portal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top