Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get null reference exception error everytime i run the program. how can i fix it? here's the code:

HTML
Protected Sub ContactNoGrid_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContactNoGrid.SelectedIndexChanged

 Me.txtContactNoID.Text = CType(ContactNoGrid.SelectedRow.FindControl("lblContactID"), Label).Text
Me.txtEditContact.Text = Convert.ToString(ContactNoGrid.SelectedRow.Cells(2).Text.Trim) 'CType(ContactNoGrid.SelectedRow.FindControl("lblContact"), Label).Text
Me.ddEditContactType.SelectedValue = Convert.ToString(ContactNoGrid.SelectedRow.Cells(1).Text.Trim)
Me.txtEditLocal.Text = IIf(Convert.ToString(ContactNoGrid.SelectedRow.Cells(3).Text.Trim) = " ", "", Convert.ToString(ContactNoGrid.SelectedRow.Cells(3).Text.Trim))
Me.btnDelContact.Visible = True  
 
''check if primary
Dim connStr As String = ConfigurationManager.ConnectionStrings("DBConn").ConnectionString
Dim sqlCon As New SqlConnection(connStr)
Dim sqlCmd As New SqlCommand("dbo.spCheckPrimaryContact", sqlCon)
Dim sqlDA As New SqlDataAdapter
Dim dt As New DataTable

sqlCmd.CommandType = Data.CommandType.StoredProcedure
sqlCmd.Parameters.AddWithValue("@iContactID", Me.txtContactNoID.Text)
sqlDA.SelectCommand = sqlCmd
sqlDA.Fill(dt)

If dt.Rows(0).Item("bPrimary") = True Then
Me.chkPrimaryContact.Checked = True
Else
Me.chkPrimaryContact.Checked = False
End If

ddlPrefix4.Enabled = True
ddlPrefix4.Visible = True
Dim sqlContactNo As New SqlCommand("dbo.spSeparateContactNumber", sqlCon)
Dim dt2 As New DataTable
sqlContactNo.CommandType = Data.CommandType.StoredProcedure
sqlContactNo.Parameters.AddWithValue("@iContactID", Me.txtContactNoID.Text)
sqlDA.SelectCommand = sqlContactNo
sqlDA.Fill(dt2)
Dim test As String = dt2.Rows(0).Item(0)
ddlPrefix4.Items.FindByText(test).Selected = True
txtEditContact.Text = dt2.Rows(0).Item(1)
End Sub


I get it at "ddlPrefix4.Items.FindByText(test).Selected = True" line

What I have tried:

I tried finding the null but i dont have one
Posted
Updated 26-Feb-17 16:25pm
Comments
[no name] 26-Feb-17 21:08pm    
"I tried finding the null but i dont have one", yes you do or you wouldn't be getting the error.
Bryian Tan 26-Feb-17 21:36pm    
How did you debug it?

1 solution

Anyway, don't be afraid to use the VS debugger. Put a break point next to the line in question. There could be a likelihood there is no null in the application but FindByText(test) could be returning null (the search value not in the list) and .selected blow it out.

Here are list of videos from Google on how to utilize the debugger

Debugger Video[^]

Here are links to article on how to use the VS debugger.
Debugging in Visual Basic .NET[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900