NullReferenceException未处理

时间:2011-06-29 22:14:04

标签: vb.net

Imports System
Imports SnmpSharpNet

Public Class Form1
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    DataGridView1.Columns.Add("NameOne", "Column One")
    DataGridView1.Columns.Add("NameTwo", "Column Two")

    Dim dataGridRow As New DataGridViewRow()
    Dim cells As DataGridViewCell() = New DataGridViewCell(1) {}
    Dim txt1A As New DataGridViewTextBoxCell()
    Dim txt1B As New DataGridViewTextBoxCell()
    txt1A.Value = "Host"
    dataGridRow.Cells.Add(txt1A)
    txt1A.[ReadOnly] = False
    dataGridRow.Cells.Add(txt1B)
    dataGridRow.Height = 20
    DataGridView1.Rows.Add(dataGridRow)



    dataGridRow = New DataGridViewRow()
    cells = New DataGridViewCell(1) {}

    Dim txt2A As New DataGridViewTextBoxCell()
    Dim cbo1 As New DataGridViewComboBoxCell()

    cbo1.Items.Add("1.3.6.1.2.1.1.1.0")
    cbo1.Items.Add("1.3.6.1.2.1.1.2.0")
    cbo1.Items.Add("1.3.6.1.2.1.1.3.0")
    cbo1.Items.Add("1.3.6.1.2.1.1.4.0")
    cbo1.Items.Add("1.3.6.1.2.1.1.5.0")

    cbo1.Value = cbo1.Items(0)

    cbo1.[ReadOnly] = False
    txt2A.Value = "OIDs"
    dataGridRow.Cells.Add(txt2A)
    txt2A.[ReadOnly] = True
    dataGridRow.Cells.Add(cbo1)
    dataGridRow.Height = 20
    DataGridView1.Rows.Add(dataGridRow)

    Dim requestOid() As String

    requestOid = New String() {cbo1.Selected}

    dataGridRow = New DataGridViewRow()
    cells = New DataGridViewCell(1) {}
    Dim txt3A As New DataGridViewTextBoxCell()
    Dim cbo2 As New DataGridViewComboBoxCell()

    cbo2.Items.Add("Get")
    cbo2.Items.Add("GetNext")


    cbo2.Value = cbo2.Items(0)

    cbo2.[ReadOnly] = False
    txt3A.Value = "SNMP Operation"
    dataGridRow.Cells.Add(txt3A)
    txt3A.[ReadOnly] = True
    dataGridRow.Cells.Add(cbo2)
    dataGridRow.Height = 20
    DataGridView1.Rows.Add(dataGridRow)

    dataGridRow = New DataGridViewRow()
    cells = New DataGridViewCell(1) {}
    Dim txt4A As New DataGridViewTextBoxCell()
    Dim txt4B As New DataGridViewTextBoxCell()
    txt4A.Value = "Community String"
    txt4B.Value = "public"
    dataGridRow.Cells.Add(txt4A)
    dataGridRow.Cells.Add(txt4B)
    dataGridRow.Height = 20
    DataGridView1.Rows.Add(dataGridRow)



End Sub
Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs)

    Dim comboControl As DataGridViewComboBoxEditingControl = TryCast(e.Control, DataGridViewComboBoxEditingControl)
    If comboControl IsNot Nothing Then
        ' Set the DropDown style to get an editable ComboBox
        If comboControl.DropDownStyle <> ComboBoxStyle.DropDown Then
            comboControl.DropDownStyle = ComboBoxStyle.DropDown
        End If
    End If
End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim result As Dictionary(Of Oid, AsnType)
    Dim requestoid() As String
    Dim cbo1 As New DataGridViewComboBoxCell()
    Dim txt1B As New DataGridViewTextBoxCell()
    Dim txt4B As New DataGridViewTextBoxCell()
    Dim host As String = txt1B.Value

    Dim community As String
    community = txt4B.Value
    requestoid = New String() {cbo1.Selected}

    Dim snmp As New SimpleSnmp
    snmp = New SimpleSnmp(host, community)

    'abc.Text = txtsnmpaction.SelectedItem
    result = snmp.Get(SnmpVersion.Ver1, requestoid)

    'result = snmp.GetNext(SnmpVersion.Ver1, requestoid)

    'If (txtsnmpaction = "GetBulk")
    'result = snmp.GetBulk(New String() {".1.3.6.1.2", ".1.3.6.1.3"})
    ' End If
    If Not snmp.Valid Then

        MessageBox.Show("Invalid hostname/community")

    End If
    If result IsNot Nothing Then
        Dim kvp As KeyValuePair(Of Oid, AsnType)
        For Each kvp In result
            MessageBox.Show("kvp.Key.ToString")
            MessageBox.Show(SnmpConstants.GetTypeName(kvp.Value.Type))
            MessageBox.Show(kvp.Value.ToString())

        Next kvp
    Else
        MessageBox.Show("No results received")
    End If



    End Sub
End Class

我收到错误在此代码行处理了NullReference异常

Dim snmp As New SimpleSnmp
        snmp = New SimpleSnmp(host, community)

请帮助我......!

1 个答案:

答案 0 :(得分:0)

nullreference是因为主机和社区字符串为空。您将它们设置为DataGridViewTextBoxCell的新实例的值,这是什么都没有。

您是否尝试将它们设置为datagridview中的特定行?

如果是这样,你应该使用类似的东西:

host = DataGridView1.Rows(1).Cells(1).Value.ToString
community = DataGridView1.Rows(1).Cells(2).Value.ToString