Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nullreferenceexception object variable or with block variable not set 1

Status
Not open for further replies.

scottaherbst

Technical User
Jan 18, 2007
46
US
Hi I'm using vb.net 2003 and have started getting this error message

"Unhandled exception of type system.nullreferenceexception object variable or with block variable not set."

It gets stopped in this procedure at the line:
Private Sub shuffle_source(ByRef image_array As Array,_ ByVal cycles As Integer)

Private Sub shuffle_source(ByRef image_array As Array,_ ByVal cycles As Integer)
Randomize()
Dim rndPosition As New Random_(DateTime.Now.Millisecond)

For i As Integer = 1 To 4

For i2 As Integer = 1 To image_array.Length_
swap(image_array(rndPosition.Next(0,_ image_array.Length)), image_array(rndPosition.Next(0,_ image_array.Length)))

Next i2

Next i
Label13.Image = image_array(0)
Label14.Image = image_array(1)
Label15.Image = image_array(2)
End Sub

What's strange is that it ran fine until I added a timer that made the screen blink when another timer wound down. Any ideas?
 
Private Sub shuffle_source(ByRef image_array As Array,_ ByVal cycles As Integer)
 
Private Sub shuffle_source(ByRef image_array As Array, >> _ << ByVal cycles As Integer)

remove this underscore and try again
 
oops..

check the object being passed to the routine to make sure it has been initialized properly
 
Actually, the underscores aren't part of the original code. I included them to demonstrate line continuation when I pasted the code into this box. Here's the code without the underscores. Same problem.

"Unhandled exception of type system.nullreferenceexception object variable or with block variable not set."

It gets stopped in this procedure at the line:
Private Sub shuffle_source(ByRef image_array As Array, ByVal cycles As Integer)

Private Sub shuffle_source(ByRef image_array As Array, ByVal cycles As Integer)
Randomize()
Dim rndPosition As New Random(DateTime.Now.Millisecond)

For i As Integer = 1 To 4

For i2 As Integer = 1 To image_array.Length
swap(image_array(rndPosition.Next(0, image_array.Length)), image_array(rndPosition.Next(0, image_array.Length)))

Next i2

Next i
Label13.Image = image_array(0)
Label14.Image = image_array(1)
Label15.Image = image_array(2)
End Sub

What's strange is that it ran fine until I added a timer that made the screen blink when another timer wound down. Any ideas?
 
Sorry, check that (array)image_array and (integer)cycles are being passed correctly.

Add a breakpoint over:
Private Sub shuffle_source(ByRef image_array As Array, ByVal cycles As Integer)

Hover your mouse over image_array and cycles at runtime to ensure they have been set at that point.
 
Thanks for the help. For some reason, it worked when I changed it from

randomize(datetime.now.milliseconds) to
randomize(now.milliseconds)
 
for future reference ad a try..catch block to your code as it may help identify your problem.

Code:
Private Sub shuffle_source(ByRef image_array As Array, ByVal cycles As Integer)
[b][u]try[/u][/b]
        Randomize()
        Dim rndPosition As New Random(DateTime.Now.Millisecond)

        For i As Integer = 1 To 4

            For i2 As Integer = 1 To image_array.Length
                swap(image_array(rndPosition.Next(0, image_array.Length)), image_array(rndPosition.Next(0, image_array.Length)))

            Next i2

        Next i
        Label13.Image = image_array(0)
        Label14.Image = image_array(1)
        Label15.Image = image_array(2)
[b][u]catch ex as exception
    messagebox.show(ex.message)
end try[/u][/b]
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top