Object Refrence not Set while New Keyword is being used
I have th开发者_JAVA百科is 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?
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.
If there is no other code between initialization and setting the CentreId
there should not be any way moMember
is null. Maybe the exception occurs during construction of CMember
.
精彩评论