By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,360 Members | 3,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,360 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.Out look.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" <jc***@macomexpress.comwrote in message
news:uX**************@TK2MSFTNGP02.phx.gbl...
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)oIt ems[i];

//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)oIt ems[i];

//Do something with oContact

oContact = null;

}

Nov 15 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

JC wrote:
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.Out look.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" <jc***@macomexpress.comwrote in message
news:uX**************@TK2MSFTNGP02.phx.gbl...
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)oIt ems[i];

//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)oIt ems[i];

//Do something with oContact

oContact = null;

}
Nov 15 '06 #2

P: n/a

"JC" <jc***@macomexpress.comwrote in message
news:ux**************@TK2MSFTNGP02.phx.gbl...
| 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.Out look.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.
Nov 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.