Faulty Orc
Faulty Orc

Reputation: 991

Object Refrence not Set while New Keyword is being used

I have this CMember Class. I Write something like

Dim moMember As CMember
moMember = New CMember

Then I want to set a variable to its CentreId property:

moMember.CentreId = CentreNumber

Both are Integers, however a Object Refrence Not Set exception is thrown, WHY?

Upvotes: 0

Views: 51

Answers (2)

Zebi
Zebi

Reputation: 8882

If there is no other code between initialization and setting the CentreId there should not be any way moMemberis null. Maybe the exception occurs during construction of CMember.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062502

My guess is that moMember is not null, but that the property setter (CentreId) uses some internal state that is my correctly configured, and is throwing the exception as a consequence. An event being invoked without a null-check would be a classic example, as would some inner object that holds the state. Check the setter to see what is happening. If the value you are assigning (CentreNumber) is a property, it could also be thrown from in there; again: check the getter.

It is possible for new on a class to return null, but only in an extreme edge case that can not happen by accident. Thus I very much doubt that moMember is null - the debugger will tell you quicky enough.

Upvotes: 1

Related Questions