NullReferenceException : Object reference not set to an instance of an object 
Author Message
 NullReferenceException : Object reference not set to an instance of an object

I wrote a .Net managed C++ class that calls the Win32 function
FormatMessageW.  I periodically get the below exception.  I am VERY confused
by this.  First, I am making a Win32 call and it is throwing a .Net
exception.  How is this possible?  Second, how do I debug this.  I can set a
break point on this line of code when this occurs (it consistently occurs
while formatting a message for a single event from an event log) and see
that the parameters I am passing are correct.

01/08/03 14:21:49.104 : T0x63 : SBException
 (0)[?No Source?] System.NullReferenceException : Object reference not set
to an instance of an object.

   at FormatMessageW(UInt32 , Void* , UInt32 , UInt32 , UInt16* , UInt32 ,
SByte** )
   at WinMon.MessageFormatter.Format(UInt32 eventId, UInt16**
replacementStrings) in c:\sandbox\cs\projects\winmon\winmo
n.cpp:line 562
   at WinMon.MessageFormatter.Format(EventLogEntry entry, String
defaultMessage) in c:\sandbox\cs\projects\winmon\winmon
..cpp:line 538
   at Foo.Monitoring.EventLogResource.GetMessage(EventLogSource
eventLogSource, EventLogEntry entry) in c:\sa
ndbox\cs\projects\_nativeproxy\eventlogutil.cs:line 369

The actual code looks like this:

count = ::FormatMessage(FORMAT_MESSAGE_FROM_HMODULE |
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY,

module,

eventId,

MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),

(LPTSTR)&lpBuffer,

0,

reinterpret_cast<va_list*>(replacementStrings));

The only thing unusual about the above is the variable replacementStrings is
an array of pointers to LPTSTR strings that point to the contents of .Net
strings.  Below is sample code of how I have done this:

LPTSTR* strings =
(LPTSTR*)(malloc(sizeof(LPVOID)*replacementStrings->Length));

for (int i=0; i<replacementStrings->Length; i++)

{

wchar_t __pin* tmp = PtrToStringChars(replacementStrings[i]);

strings[i] = tmp;

Quote:
}

Any thoughts?  Help is appreciated.  It occurs infrequently but quite
consistently.

Thanks in advance.

Joe



Mon, 27 Jun 2005 03:38:55 GMT  
 NullReferenceException : Object reference not set to an instance of an object
Joe,
The problem is most likley with your code below:

{

wchar_t __pin* tmp = PtrToStringChars(replacementStrings[i]);

strings[i] = tmp;

Quote:
} //tmp goes out of scope here.

When temp goes out of scope, the inside pointer returned by
PtrToStringChars is no longer pinned and it can be moved in the heap by the
garbage  collector.

Are  the format FormatMessage call and the pinning code above in the same
function?
Then you could create a pinning pointer to just  one  element  (say the
first element) of the replacementStrings array in a scope that
encompasses the FormatMessage call and  you should be fine.
(Pinning one element of an array pins the entire array).

If that is not the case, for example the calls happen in two different
functions, you may have to  allocate memory  and copy the string   pointed
to by the pinned pointer before it gets out of scope.

Hope this helps.

Thank you,
Bobby Mattappally
Microsoft VC++/C# Team

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------

Quote:


>Subject: NullReferenceException : Object reference not set to an instance
of an object
>Date: Wed, 8 Jan 2003 14:38:55 -0500
>Lines: 70

>I wrote a .Net managed C++ class that calls the Win32 function
>FormatMessageW.  I periodically get the below exception.  I am VERY
confused
>by this.  First, I am making a Win32 call and it is throwing a .Net
>exception.  How is this possible?  Second, how do I debug this.  I can set
a
>break point on this line of code when this occurs (it consistently occurs
>while formatting a message for a single event from an event log) and see
>that the parameters I am passing are correct.

>01/08/03 14:21:49.104 : T0x63 : SBException
> (0)[?No Source?] System.NullReferenceException : Object reference not set
>to an instance of an object.

>   at FormatMessageW(UInt32 , Void* , UInt32 , UInt32 , UInt16* , UInt32 ,
>SByte** )
>   at WinMon.MessageFormatter.Format(UInt32 eventId, UInt16**
>replacementStrings) in c:\sandbox\cs\projects\winmon\winmo
>n.cpp:line 562
>   at WinMon.MessageFormatter.Format(EventLogEntry entry, String
>defaultMessage) in c:\sandbox\cs\projects\winmon\winmon
>.cpp:line 538
>   at Foo.Monitoring.EventLogResource.GetMessage(EventLogSource
>eventLogSource, EventLogEntry entry) in c:\sa
>ndbox\cs\projects\_nativeproxy\eventlogutil.cs:line 369

>The actual code looks like this:

>count = ::FormatMessage(FORMAT_MESSAGE_FROM_HMODULE |
>FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY,

>module,

>eventId,

>MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),

>(LPTSTR)&lpBuffer,

>0,

>reinterpret_cast<va_list*>(replacementStrings));

>The only thing unusual about the above is the variable replacementStrings
is
>an array of pointers to LPTSTR strings that point to the contents of .Net
>strings.  Below is sample code of how I have done this:

>LPTSTR* strings =
>(LPTSTR*)(malloc(sizeof(LPVOID)*replacementStrings->Length));

>for (int i=0; i<replacementStrings->Length; i++)

>{

>wchar_t __pin* tmp = PtrToStringChars(replacementStrings[i]);

>strings[i] = tmp;

>}

>Any thoughts?  Help is appreciated.  It occurs infrequently but quite
>consistently.

>Thanks in advance.

>Joe



Mon, 27 Jun 2005 06:52:24 GMT  
 NullReferenceException : Object reference not set to an instance of an object

Indeed the Exception is not friendly for trace problem. The real reason was
some wrong parameter been set for the API. You need check whether you give a
correct API define then the value for  parameter with the pointer type.


Quote:

> I wrote a .Net managed C++ class that calls the Win32 function
> FormatMessageW.  I periodically get the below exception.  I am VERY
confused
> by this.  First, I am making a Win32 call and it is throwing a .Net
> exception.  How is this possible?  Second, how do I debug this.  I can set
a
> break point on this line of code when this occurs (it consistently occurs
> while formatting a message for a single event from an event log) and see
> that the parameters I am passing are correct.

> 01/08/03 14:21:49.104 : T0x63 : SBException
>  (0)[?No Source?] System.NullReferenceException : Object reference not set
> to an instance of an object.

>    at FormatMessageW(UInt32 , Void* , UInt32 , UInt32 , UInt16* , UInt32 ,
> SByte** )
>    at WinMon.MessageFormatter.Format(UInt32 eventId, UInt16**
> replacementStrings) in c:\sandbox\cs\projects\winmon\winmo
> n.cpp:line 562
>    at WinMon.MessageFormatter.Format(EventLogEntry entry, String
> defaultMessage) in c:\sandbox\cs\projects\winmon\winmon
> .cpp:line 538
>    at Foo.Monitoring.EventLogResource.GetMessage(EventLogSource
> eventLogSource, EventLogEntry entry) in c:\sa
> ndbox\cs\projects\_nativeproxy\eventlogutil.cs:line 369

> The actual code looks like this:

> count = ::FormatMessage(FORMAT_MESSAGE_FROM_HMODULE |
> FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY,

> module,

> eventId,

> MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),

> (LPTSTR)&lpBuffer,

> 0,

> reinterpret_cast<va_list*>(replacementStrings));

> The only thing unusual about the above is the variable replacementStrings
is
> an array of pointers to LPTSTR strings that point to the contents of .Net
> strings.  Below is sample code of how I have done this:

> LPTSTR* strings =
> (LPTSTR*)(malloc(sizeof(LPVOID)*replacementStrings->Length));

> for (int i=0; i<replacementStrings->Length; i++)

> {

> wchar_t __pin* tmp = PtrToStringChars(replacementStrings[i]);

> strings[i] = tmp;

> }

> Any thoughts?  Help is appreciated.  It occurs infrequently but quite
> consistently.

> Thanks in advance.

> Joe



Mon, 27 Jun 2005 17:14:28 GMT  
 NullReferenceException : Object reference not set to an instance of an object


Quote:
> Are  the format FormatMessage call and the pinning code above in the
> same function?
> Then you could create a pinning pointer to just  one  element  (say
> the first element) of the replacementStrings array in a scope that
> encompasses the FormatMessage call and  you should be fine.
> (Pinning one element of an array pins the entire array).

Bobby,

That won't help as the array contains *whole object pointers* to the
System::String objects.  While the array can't move if any element in the
array is pinned the strings are not in the array, so this would not help.

--
Jeff Peil



Tue, 28 Jun 2005 18:26:23 GMT  
 NullReferenceException : Object reference not set to an instance of an object

Quote:


>Subject: Re: NullReferenceException : Object reference not set to an

instance of an object

Quote:

>Bobby,

>That won't help as the array contains *whole object pointers* to the
>System::String objects.  While the array can't move if any element in the
>array is pinned the strings are not in the array, so this would not help.

That's right! I stand corrected.

Thank you,
Bobby Mattappally
Microsoft VC++/C# Team

This posting is provided "AS IS" with no warranties, and confers no rights.



Wed, 29 Jun 2005 07:21:13 GMT  
 
 [ 5 post ] 

 Relevant Pages 

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

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

3. Linklabel problem Object reference not set to an instance of an object

4. Object reference not set to an instance of an object

5. Object reference not set to an instance of an object

6. Error: Object reference not set to an instance of an object

7. Object refernce not set to an instance of an object

8. How to Convert the Name of an Object Instance Into an Object Reference

9. registerhiddenfield - object reference not set ...

10. Storing Object instances and reteriving Object instances

11. Object reference not set to an instance of an object.

12. Object reference not set to an instance of an object.

 

 
Powered by phpBB® Forum Software