Object Variable or With block variable not set.

  • Thread starter Thread starter Ross Culver
  • Start date Start date
R

Ross Culver

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
 
I haven't seen this error since VB 6. Do you have option strict turned on
(if this is a VB .NET app)?
 
Just for hoots, I changed the Option Explicit, Option Strict settings to
every possible combination. None resolved this problem.

Ross
 
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
 
Ross Culver said:
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*.
 
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
 
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?
 
I couldn't figure it out, so I just put "'Do nothing" in the catch phrase to
ignore the error.

Ross
 
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
 
Back
Top