perpetual_dream

Hello..............

I have a linklabel that links to a form(contacts) with a datagridview....

Private Sub Linknewcontacts_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles Linknewcontacts.LinkClicked

Try

Dim frm As New contacts

frm.ShowDialog()

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub

The above code works just fine. However, in the contacts form I have the following event code....

Code Snippet

Private Sub dgcontacts_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgcontacts.MouseClick

contactdetails.contact_id = Me.dgcontacts.CurrentRow.Cells("contact_id").Value.ToString

If e.Button = Windows.Forms.MouseButtons.Right Then ''' here an error which rises the exception ex in the above code

Me.ContextMenudgcontacts.Show(dgcontacts, e.Location)

End If

End Sub

This code in the form contacts raises an error "object reference not set an instance to an object error" as an exception to Linknewcontacts.LinkClicked

I donno Why.......

THNXXXXXXXXX



Re: Visual Basic Express Edition a weird object ref not set an instance error

js06

The only time i have seen an error like this is when i had quite a few variables declared in my form.

It didn't like where i had placed the variable code for some reason.

What i had to do to figure it out was to make another form that i opened with a button.

i put one part of the code in at a time to test it

example

first i put in the code for the load event and tried to run it

it didn't work

so then i put the rest of the code in minus the load event code

then it worked

so then i found out that it did not like the variables that were loaded when the form was opened

i had to place them at the class level outside all subs and then reference them in my subs

Public Class Endform

Dim sname1 As String = My.Settings.name1

Dim sname2 As String = My.Settings.name2

Dim sname3 As String = My.Settings.name3

in total i had 20 declared variables

then i had all my subs below

I dont' know if this will have anything to do with your problem but this is the only time i have seen this error

The funny thing about it was i don't recall seeing the error until i tried to use the showdialog() command instead of show()

But after i did redid my code, it didn't make any difference if i used one or the other

Anyway, hope this helps you






Re: Visual Basic Express Edition a weird object ref not set an instance error

perpetual_dream

I used show instead of showdialoig which gave me the chance to view the error.... I used Me.dgcontacts.SelectedRows.Item(0).Cells("contact_id").Value instead of Me.dgcontacts.currentrow.Cells("contact_id").Value which works just fine if I didn't open the form thru the link label. Weird!

Code Snippet

Private Sub dgcontacts_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgcontacts.MouseClick

Me.dgcontacts.SelectionMode = DataGridViewSelectionMode.FullRowSelect

contactdetails.contact_id = Me.dgcontacts.SelectedRows.Item(0).Cells("contact_id").Value

If e.Button = Windows.Forms.MouseButtons.Right Then

Me.ContextMenudgcontacts.Show(dgcontacts, e.Location)

End If

End Sub

thnx js06





Re: Visual Basic Express Edition a weird object ref not set an instance error

nobugz

You have two opportunities in your original code to generate this exception. Either CurrentRow or Value might be Nothing. When the exception happens, .NET goes looking for a Catch clause and it finds one in the LinkClicked event handler. When you use the Show() method, LinkClicked is no longer active and the debugger gets a chance to show you what went wrong because there is no Catch clause that can handle the exception.

Catching "Exception" is almost always a bad idea. You'll catch and dismiss the really nasty exceptions like StackOverflow, OutOfMemory and ExecutionEngineException, exceptions that really ought to terminate your program because things went horribly wrong. Your program will both be more reliable and easier to debug if you simply remove the Try/Catch statements.