problem whith C# Microsoft.Office.Interop.Outlook namespace (null reference)

J

JC

Anybody knows what problem has this code? I think, in the Garbage
Collector? You know the Solution?
The program in the test's case, whit 350 contacts, run OK before number
86. The error is a "Array index out of bounds".


Microsoft.Office.Interop.Outlook._Application olApp = new
Microsoft.Office.Interop.Outlook.ApplicationClass();
Microsoft.Office.Interop.Outlook._NameSpace olNs =
olApp.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook.MAPIFolder aContacts =
olNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
Microsoft.Office.Interop.Outlook.Items oItems = aContacts.Items;
FAddressBookName = aContacts.AddressBookName;
foreach (Microsoft.Office.Interop.Outlook._ContactItem oContact in
oItems)
{

//do something
}
 
J

justin creasy

If you think the problem is the Garbage Collector is not collecting
memory that is free, you can programmatically instruct the GC to
collect the garbage with the following line of code.

GC.Collect();

That said, I don't think a problem with the GC would cause the error
message you saw. Can you post what happens in the foreach loop b/c it
seems to me that is where you are overreaching your array size.
Anybody knows what problem has this code? I think, in the Garbage
Collector? You know the Solution?
The program in the test's case, whit 350 contacts, run OK before number
86. The error is a "Array index out of bounds".


Microsoft.Office.Interop.Outlook._Application olApp = new
Microsoft.Office.Interop.Outlook.ApplicationClass();
Microsoft.Office.Interop.Outlook._NameSpace olNs =
olApp.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook.MAPIFolder aContacts =
olNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
Microsoft.Office.Interop.Outlook.Items oItems = aContacts.Items;
FAddressBookName = aContacts.AddressBookName;
foreach (Microsoft.Office.Interop.Outlook._ContactItem oContact in
oItems)
{

//do something
}



JC said:
Hi...

Anybody knows what problem has this code? I think, in the Garbage
Collector? You know the Solution?

The program in the test's case, whit 350 contacts, run OK before number
86. The error is a "Array index out of bounds".



Microsoft.Office.Interop.Outlook._Application olApp = new
Microsoft.Office.Interop.Outlook.ApplicationClass();

Microsoft.Office.Interop.Outlook._NameSpace olNs =
olApp.GetNamespace("MAPI");

Microsoft.Office.Interop.Outlook._Folders oFolders =
olNs.Folders;

Microsoft.Office.Interop.Outlook.MAPIFolder aContacts =
olNs.PickFolder();

Microsoft.Office.Interop.Outlook.Items oItems =
aContacts.Items;

for (int i = 0; i <= x; oItems.Count)

{ //Explota en la proxima linea.

Microsoft.Office.Interop.Outlook._ContactItem oContact =
(Microsoft.Office.Interop.Outlook._ContactItem)oItems;

//Do something with oContact

oContact = null;

}



In this second case, the error appear in the line before the "for".



Microsoft.Office.Interop.Outlook._Application olApp = new
Microsoft.Office.Interop.Outlook.ApplicationClass();

Microsoft.Office.Interop.Outlook._NameSpace olNs =
olApp.GetNamespace("MAPI");

Microsoft.Office.Interop.Outlook._Folders oFolders =
olNs.Folders;

Microsoft.Office.Interop.Outlook.MAPIFolder aContacts =
olNs.PickFolder();

Microsoft.Office.Interop.Outlook.Items oItems =
aContacts.Items;

int x = oItems.Count;

//Explota en la proxima linea.

for (int i = 0; i <= x; i++)

{

Microsoft.Office.Interop.Outlook._ContactItem oContact =
(Microsoft.Office.Interop.Outlook._ContactItem)oItems;

//Do something with oContact

oContact = null;

}
 
W

Willy Denoyette [MVP]

| Anybody knows what problem has this code? I think, in the Garbage
| Collector? You know the Solution?
| The program in the test's case, whit 350 contacts, run OK before number
| 86. The error is a "Array index out of bounds".
|
|
| Microsoft.Office.Interop.Outlook._Application olApp = new
| Microsoft.Office.Interop.Outlook.ApplicationClass();
| Microsoft.Office.Interop.Outlook._NameSpace olNs =
| olApp.GetNamespace("MAPI");
| Microsoft.Office.Interop.Outlook.MAPIFolder aContacts =
|
olNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
| Microsoft.Office.Interop.Outlook.Items oItems = aContacts.Items;
| FAddressBookName = aContacts.AddressBookName;
| foreach (Microsoft.Office.Interop.Outlook._ContactItem oContact in
| oItems)
| {
|
| //do something
| }
|
|

Forget about the GC, just run your code through the debugger and watch the
index when it throws. Anyway, the piece of code you post is not the cause,
it's the "do something" which goes wrong

Willy.
 

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

Top