Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / October 2007

Tip: Looking for answers? Try searching our database.

Object Variable or With block variable not set.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ross Culver - 19 Oct 2007 16:49 GMT
I get this error whenever I close one particular form in my Windows
application; however, I can't find the error in the code.  Setting a break
on the dispose event doesn't catch it and the error does not occur on any
other event on the form.  I've unchecked the 'My Code only' debug option,
but I still can't catch this error.

Any suggestions?

Thanks.

Ross
Scott M. - 19 Oct 2007 17:42 GMT
I haven't seen this error since VB 6.  Do you have option strict turned on
(if this is a VB .NET app)?

>I get this error whenever I close one particular form in my Windows
>application; however, I can't find the error in the code.  Setting a break
[quoted text clipped - 7 lines]
>
> Ross
Ross Culver - 19 Oct 2007 17:47 GMT
VS2K5, Option Strict was Off.

>I haven't seen this error since VB 6.  Do you have option strict turned on
>(if this is a VB .NET app)?
[quoted text clipped - 10 lines]
>>
>> Ross
Ross Culver - 19 Oct 2007 17:56 GMT
Just for hoots, I changed the Option Explicit, Option Strict settings to
every possible combination.  None resolved this problem.

Ross

>I haven't seen this error since VB 6.  Do you have option strict turned on
>(if this is a VB .NET app)?
[quoted text clipped - 10 lines]
>>
>> Ross
Scott M. - 19 Oct 2007 22:09 GMT
You should always work with Option Explicit and Option Strict set to ON
(explicit is on by default but strict if off by default).

With Option Strict On you are forced to write more succinct code, but the
payoff is less runtime errors.  I was hoping that by turning it on, you'd
see your error during the build.

This error simply means that you have a variable that is referring to an
ojbect that has not been instantiated, like this:

Dim sb As System.Text.StringBuilder
sb.Append("Test")

In this case sb is just a variable refering to a memory address that "will
eventually" hold an actual instance of a StringBuilder, but without the
keyword "New" in the declaration line, you don't actually have an instance
of one, thus  you have an "Object reference not set to an instance of an
object."

Setting a break on dispose wouldn't catch the problem because dispose occurs
so late in the process.  I'd set a breakpoint in the Load event and then
step through the normal usage of the form, you'll find the error when you
close the form.

-Scott

> Just for hoots, I changed the Option Explicit, Option Strict settings to
> every possible combination.  None resolved this problem.
[quoted text clipped - 15 lines]
>>>
>>> Ross
Mr. Arnold - 20 Oct 2007 14:55 GMT
>I get this error whenever I close one particular form in my Windows
>application; however, I can't find the error in the code.  Setting a break
>on the dispose event doesn't catch it and the error does not occur on any
>other event on the form.  I've unchecked the 'My Code only' debug option,
>but I still can't catch this error.

You have a With statement with no end-with or the other way around. Or you
are dereferencing an object variable that was never initialized. You'll
notice the words (not set). I'll assume this is VB.Net

Debug->Exceptions

Select "Common Runtime Exceptions"

Caption at top of screen "Break when an exception is:    and you will
select *Thrown*, This is a break on *All Errors*.
Ross Culver - 22 Oct 2007 18:38 GMT
Actually, this line of code is the problem:

DocType = CType(Me.lbSearchDocType.SelectedItem(0).ToString, Integer)

DocType has been declared public integer.  The lbSearchDocType is a simple
Windows listbox with selection mode set to 'One'.

The code does return the correct value, so why do I get this error when
closing the form?

Ross

>>I get this error whenever I close one particular form in my Windows
>>application; however, I can't find the error in the code.  Setting a break
[quoted text clipped - 12 lines]
> Caption at top of screen "Break when an exception is:    and you will
> select *Thrown*, This is a break on *All Errors*.
Jack Jackson - 22 Oct 2007 18:58 GMT
Where is that line of code?  I would guess that the error occurs
because at the time that line is executed the listbox has no item
selected.

Since you don't mention the debugger, I assume this happens when
running a Release build.  Does it fail when run under the debugger?

>Actually, this line of code is the problem:
>
[quoted text clipped - 24 lines]
>> Caption at top of screen "Break when an exception is:    and you will
>> select *Thrown*, This is a break on *All Errors*.
Ross Culver - 22 Oct 2007 19:04 GMT
It's in the SelectedIndexChanged event.  Go figure!

Ross

> Where is that line of code?  I would guess that the error occurs
> because at the time that line is executed the listbox has no item
[quoted text clipped - 35 lines]
>>> Caption at top of screen "Break when an exception is:    and you will
>>> select *Thrown*, This is a break on *All Errors*.
Jack Jackson - 22 Oct 2007 21:43 GMT
You didn't answer the rest of my questions.  Does this not happen when
run under the debugger?

I still think it happens because the code expects there to be
something selected in the listbox.  If nothing is selected this is
what will happen.

Either put a breakpoint on the line and see what
lbSearchDocType.SelectedItem.Count is, or change the code to:

If Me.lbSearchDocType.Count > 0 Then
 DocType = CType(Me.lbSearchDocType.SelectedItem(0).ToString,
Integer)
 ... other code that processes DocTyipe
EndIf

>It's in the SelectedIndexChanged event.  Go figure!
>
[quoted text clipped - 39 lines]
>>>> Caption at top of screen "Break when an exception is:    and you will
>>>> select *Thrown*, This is a break on *All Errors*.
Ross Culver - 22 Oct 2007 19:03 GMT
I couldn't figure it out, so I just put "'Do nothing" in the catch phrase to
ignore the error.

Ross

>I get this error whenever I close one particular form in my Windows
>application; however, I can't find the error in the code.  Setting a break
[quoted text clipped - 7 lines]
>
> Ross

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.