你调用的对象是空的。 vb.net对于一些但不是所有code [英] object reference not set to an instance of an object. vb.net for some but not all code

查看:235
本文介绍了你调用的对象是空的。 vb.net对于一些但不是所有code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在VB.NET执行我的code,我得到的对象引用不设置到对象的实例。 vb.net。

When i execute my code in VB.NET, I get the 'object reference not set to an instance of an object. vb.net'.

我有一些项目的下拉列表。在一个项目中,code的执行没有任何问题。然而,任何其他项目,我收到错误。

I have a dropdown list with a number of items. On one item, the code executes without any issues. However, on any of the other items, I receive the error.

例如,对于ddlItemCat.SelectedValue =手机/智能手机和放大器;附件鳕eexecutes正确的,但对于ddlItemCat.SelectedValue =移动计算和放大器;附件,你会得到错误

for example, for ddlItemCat.SelectedValue = "Mobile/Smart Phone & Accessories" the cod eexecutes correctly, but for ddlItemCat.SelectedValue = "Mobile Computing & Accessories" you will get the error.

Function validateInput() As Boolean
        Dim validInput As Boolean = True
        Dim backColour As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml("#F078A0")
        Dim backColourReset As System.Drawing.Color = Drawing.Color.White
        Dim errorList As String = "<span id=""errlstHdr"" style=""font-size: medium; font-weight: bold; text-align: left"">The following items must be corrected before the IT Shop Form data can be saved<span/><br /><br /><span id=""errlstDtl"" style=""font-size: small; font-weight: normal;text-align: left"">"
        Dim i As Integer
        Dim fieldName As String
        Dim tb As TextBox

        tbSurname.BackColor = backColourReset
        tbFirstname.BackColor = backColourReset
        tbPosition.BackColor = backColourReset
        tbDept.BackColor = backColourReset
        ddlCampus.BackColor = backColourReset
        tbBuilding.BackColor = backColourReset
        tbFlr.BackColor = backColourReset
        tbTelPager.BackColor = backColourReset
        ddlItemCat.BackColor = backColourReset
        msgText.Text = ""

        If chkTxt(tbSurname.Text) = False Then
            tbSurname.BackColor = backColour
            errorList += Chr(149) + "Contact Surname not entered<br />"
            validInput = False
        End If
        If chkTxt(tbFirstname.Text) = False Then
            tbFirstname.BackColor = backColour
            errorList += Chr(149) + "Contact First Name not entered<br />"
            validInput = False
        End If
        If chkTxt(tbPosition.Text) = False Then
            tbPosition.BackColor = backColour
            errorList += Chr(149) + "Contact Position not entered<br />"
            validInput = False
        End If
        If chkTxt(tbDept.Text) = False Then
            tbDept.BackColor = backColour
            errorList += Chr(149) + "Contact Department not entered<br />"
            validInput = False
        End If
        If chkTxt(ddlCampus.SelectedValue) = False Then
            ddlCampus.BackColor = backColour
            errorList += Chr(149) + "Contact Campus not entered<br />"
            validInput = False
        End If
        If chkTxt(tbBuilding.Text) = False Then
            tbBuilding.BackColor = backColour
            errorList += Chr(149) + "Contact Building not entered<br />"
            validInput = False
        End If
        If chkTxt(tbFlr.Text) = False Then
            tbFlr.BackColor = backColour
            errorList += Chr(149) + "Contact Floor not entered<br />"
            validInput = False
        End If
        If chkTxt(tbTelPager.Text) = False Then
            tbTelPager.BackColor = backColour
            errorList += Chr(149) + "Contact Phone or Pager not entered<br />"
            validInput = False
        End If
        If chkTxt(ddlItemCat.SelectedValue) = False Then
            ddlItemCat.BackColor = backColour
            errorList += Chr(149) + "Item Category not selected<br />"
            validInput = False
        End If


        If ddlItemCat.SelectedValue = "Mobile/Smart Phone & Accessories" Then
            If chkTxt(ddlNewService3.SelectedValue) = False Then
                ddlNewService3.BackColor = backColour
                errorList += Chr(149) + "New Service not selected<br />"
                validInput = False
            End If
            For i = 1 To ddlQty3.SelectedValue
                If ddlNewService3.SelectedValue = "No" Then
                    fieldName = "tbMobNo3" + i.ToString
                    tb = TryCast(FindControl(fieldName), TextBox)
                    If Not IsNothing(tb) And chkTxt(tb.Text) = False Then
                        tb.BackColor = backColour
                        errorList += Chr(149) + "Service Mobile Number not entered<br />"
                        validInput = False
                    End If
                End If
                fieldName = "tbSurname3" + i.ToString
                tb = TryCast(FindControl(fieldName), TextBox)
                If Not IsNothing(tb) And chkTxt(tb.Text) = False Then
                    tb.BackColor = backColour
                    errorList += Chr(149) + "User Surname not entered<br />"
                    validInput = False
                End If
                fieldName = "tbFirstname3" + i.ToString
                tb = TryCast(FindControl(fieldName), TextBox)
                If Not IsNothing(tb) And chkTxt(tb.Text) = False Then
                    tb.BackColor = backColour
                    errorList += Chr(149) + "User First Name not entered<br />"
                    validInput = False
                End If
            Next i
        End If

        If ddlItemCat.SelectedValue = "Mobile Computing & Accessories" Then
            If chkTxt(ddlOldDevice4.SelectedValue) = False Then
                ddlOldDevice4.BackColor = backColour
                errorList += Chr(149) + "New Service not selected<br />"
                validInput = False
            End If
            For i = 1 To tbQty4.SelectedValue
                If ddlOldDevice4.SelectedValue = "Yes" Then
                    fieldName = "tbTelPager4" + i.ToString
                    tb = TryCast(FindControl(fieldName), TextBox)
            ‘THIS IS WHERE THE ERROR OCCURS ON NEXT LINE                    
If Not IsNothing(tb) And chkTxt(tb.Text) = False Then
                        tb.BackColor = backColour
                        errorList += Chr(149) + "Service Mobile Number not entered<br />"
                        validInput = False
                    End If
                End If

                fieldName = "tbFirstname4" + i.ToString
                tb = TryCast(FindControl(fieldName), TextBox)
                If Not IsNothing(tb) And chkTxt(tb.Text) = False Then
                    tb.BackColor = backColour
                    errorList += Chr(149) + "User First Name not entered<br />"
                    validInput = False
                End If
                fieldName = "tbSurname4" + i.ToString
                tb = TryCast(FindControl(fieldName), TextBox)
                If Not IsNothing(tb) And chkTxt(tb.Text) = False Then
                    tb.BackColor = backColour
                    errorList += Chr(149) + "User Surname not entered<br />"
                    validInput = False
                End If
            Next i
        End If

        msgText.Text = errorList + "<span/>"
        Return validInput
    End Function

请帮忙

下面是code产生的细胞中的字段:

Here is the code which generates the cells for the fields:

Sub addRowsToMobComputingTable()
        Dim tbChk As TextBox
        Dim fieldName As String

        Dim rowCount As Integer = tbQty4.SelectedValue
        If rowCount = 1 Then ' additional rows are not required
            Return
        End If
        For i = 2 To rowCount
            ' check to ensure field doesn't exist
            fieldName = "tbSurname4" + i.ToString
            tbChk = TryCast(FindControl(fieldName), TextBox)
            If IsNothing(tbChk) Then
                For j = 0 To 10
                    Dim tempRow As New TableRow
                    Dim tempCell1 As New TableCell
                    Dim tempCell2 As New TableCell
                    Dim tempCell3 As New TableCell
                    Dim tb As New TextBox
                    Dim lbl As New Label
                    Try
                        Select Case j

                            Case 0
                                tempCell1.Text = ""
                                tempCell1.CssClass = "style8"
                                tempRow.Cells.Add(tempCell1)
                                tempCell2.Text = "-----------------"
                                tempCell2.CssClass = "style8"
                                tempRow.Cells.Add(tempCell2)
                                tempCell3.Text = ""
                                tempCell3.CssClass = "style8"
                                tempRow.Cells.Add(tempCell3)
                                tblMobComp.Rows.Add(tempRow)
                            Case 1
                                tempCell1.Text = ""
                                tempRow.Cells.Add(tempCell1)
                                tempCell2.Text = "Surname"
                                tempCell2.CssClass = "style8"
                                tempRow.Cells.Add(tempCell2)
                                tb.Width = 144
                                tb.ID = "tbSurname4" + i.ToString
                                tempCell3.Controls.Add(tb)
                                tempRow.Cells.Add(tempCell3)
                                tblMobComp.Rows.Add(tempRow)
                            Case 2
                                tempCell1.Text = ""
                                tempRow.Cells.Add(tempCell1)
                                tempCell2.Text = "First Name"
                                tempCell2.CssClass = "style8"
                                tempRow.Cells.Add(tempCell2)
                                tb.Width = 142
                                tb.ID = "tbFirstname4" + i.ToString
                                tempCell3.Controls.Add(tb)
                                tempRow.Cells.Add(tempCell3)
                                tblMobComp.Rows.Add(tempRow)
                            Case 3
                                tempCell1.Text = ""
                                tempRow.Cells.Add(tempCell1)
                                tempCell2.Text = "Contact No."
                                tempCell2.CssClass = "style8"
                                tempRow.Cells.Add(tempCell2)
                                tb.Width = 186
                                tb.ID = "tbTelPager4" + i.ToString
                                tempCell3.Controls.Add(tb)
                                tempRow.Cells.Add(tempCell3)
                                tblMobComp.Rows.Add(tempRow)
                            Case 4
                                tempCell1.Text = ""
                                tempCell1.CssClass = "style8"
                                tempRow.Cells.Add(tempCell1)
                                tempCell2.Text = "Position"
                                tempCell2.CssClass = "style8"
                                tempRow.Cells.Add(tempCell2)
                                tb.Width = 295
                                tb.ID = "tbPosition4" + i.ToString
                                tempCell3.Controls.Add(tb)
                                tempRow.Cells.Add(tempCell3)
                                tblMobComp.Rows.Add(tempRow)
                            Case 5
                                tempCell1.Text = ""
                                tempRow.Cells.Add(tempCell1)
                                tempCell2.Text = "Department"
                                tempCell2.CssClass = "style8"
                                tempRow.Cells.Add(tempCell2)
                                tb.Width = 252
                                tb.ID = "tbDept4" + i.ToString
                                tempCell3.Controls.Add(tb)
                                tempRow.Cells.Add(tempCell3)
                                tblMobComp.Rows.Add(tempRow)
                            Case 6
                                tempCell1.Text = ""
                                tempRow.Cells.Add(tempCell1)
                                tempCell2.Text = "Campus"
                                tempCell2.CssClass = "style8"
                                tempRow.Cells.Add(tempCell2)
                                tb.Width = 252
                                tb.ID = "ddlCampus4" + i.ToString
                                tempCell3.Controls.Add(tb)
                                tempRow.Cells.Add(tempCell3)
                                tblMobComp.Rows.Add(tempRow)
                            Case 7
                                tempCell1.Text = ""
                                tempRow.Cells.Add(tempCell1)
                                tempCell2.Text = "Building"
                                tempCell2.CssClass = "style8"
                                tempRow.Cells.Add(tempCell2)
                                tb.Width = 252
                                tb.ID = "tbBuilding4" + i.ToString
                                tempCell3.Controls.Add(tb)
                                tempRow.Cells.Add(tempCell3)
                                tblMobComp.Rows.Add(tempRow)
                            Case 8
                                tempCell1.Text = ""
                                tempRow.Cells.Add(tempCell1)
                                tempCell2.Text = "Floor"
                                tempCell2.CssClass = "style8"
                                tempRow.Cells.Add(tempCell2)
                                tb.Width = 252
                                tb.ID = "tbFlr4" + i.ToString
                                tempCell3.Controls.Add(tb)
                                tempRow.Cells.Add(tempCell3)
                                tblMobComp.Rows.Add(tempRow)
                            Case 9
                                tempCell1.Text = ""
                                tempRow.Cells.Add(tempCell1)
                                lbl.ID = "lblAsset4" + i.ToString
                                lbl.Text = "Asset No. of old device"
                                If ddlOldDevice4.SelectedValue = "Yes" Then
                                    lbl.Visible = True
                                Else
                                    lbl.Visible = False
                                End If
                                tempCell2.CssClass = "style8"
                                tempCell2.Controls.Add(lbl)
                                tempRow.Cells.Add(tempCell2)
                                tb.Width = 144
                                If ddlOldDevice4.SelectedValue = "Yes" Then
                                    tb.Visible = True
                                Else
                                    tb.Visible = False
                                End If
                                tb.ID = "tbAsset4" + i.ToString
                                tempCell3.Controls.Add(tb)
                                tempRow.Cells.Add(tempCell3)
                                tblMobComp.Rows.Add(tempRow)
                            Case 10
                                tempCell1.Text = ""
                                tempRow.Cells.Add(tempCell1)
                                tempCell2.Text = "Additional comments"
                                tempCell2.CssClass = "style8"
                                tempRow.Cells.Add(tempCell2)
                                tb.Width = 340
                                tb.Rows = 4
                                tb.TextMode = TextBoxMode.MultiLine
                                tb.ID = "tbComments4" + i.ToString
                                tempCell3.Controls.Add(tb)
                                tempRow.Cells.Add(tempCell3)
                                tblMobComp.Rows.Add(tempRow)
                        End Select
                    Catch
                    End Try
                Next j
            End If
        Next i

    End Sub

以上code通过一个ASP表通过日tbQty4DropDownList控件执行

<div id="Mobile Computing"> <asp:Panel ID="pnlMobComp" runat="server" Visible="False"> <span class="style5">Mobile Computing</span><br /> <br /> <asp:Table ID="tblMobComp" runat="server" Style="width: 46%;" ViewStateMode="Enabled"> <asp:TableRow> <asp:TableCell CssClass="style8"> &nbsp;</asp:TableCell> <asp:TableCell CssClass="style4"> Quantity required?</asp:TableCell> <asp:TableCell> <asp:DropDownList ID="tbQty4" runat="server" OnSelectedIndexChanged="addRowsToMobComputingTable" AutoPostBack="True"> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> <asp:ListItem>4</asp:ListItem> <asp:ListItem>5</asp:ListItem> <asp:ListItem>6</asp:ListItem> <asp:ListItem>7</asp:ListItem> <asp:ListItem>8</asp:ListItem> <asp:ListItem>9</asp:ListItem> <asp:ListItem>10</asp:ListItem> <asp:ListItem>11</asp:ListItem> <asp:ListItem>12</asp:ListItem> <asp:ListItem>13</asp:ListItem> <asp:ListItem>14</asp:ListItem> <asp:ListItem>15</asp:ListItem> <asp:ListItem>16</asp:ListItem> <asp:ListItem>17</asp:ListItem> <asp:ListItem>18</asp:ListItem> <asp:ListItem>19</asp:ListItem> <asp:ListItem>20</asp:ListItem> </asp:DropDownList> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell CssClass="style8"> &nbsp;</asp:TableCell> <asp:TableCell CssClass="style4"> Replacing an old device?</asp:TableCell> <asp:TableCell> <asp:DropDownList ID="ddlOldDevice4" runat="server" AutoPostBack="True" CssClass="style2" OnSelectedIndexChanged="ddlOldDevice4_SelectedIndexChanged"> <asp:ListItem>&lt;--- Select ---&gt;</asp:ListItem> <asp:ListItem>No</asp:ListItem> <asp:ListItem>Yes</asp:ListItem> </asp:DropDownList> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell CssClass="style8"> &nbsp;</asp:TableCell> <asp:TableCell CssClass="style4"> Who will be using this device?</asp:TableCell> <asp:TableCell> &nbsp;</asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell CssClass="style8"> &nbsp;</asp:TableCell> <asp:TableCell CssClass="style4"> Surname</asp:TableCell> <asp:TableCell> <asp:TextBox ID="tbSurname4" runat="server" Width="144px"></asp:TextBox> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell CssClass="style8"> &nbsp;</asp:TableCell> <asp:TableCell CssClass="style4"> First Name</asp:TableCell> <asp:TableCell> <asp:TextBox ID="tbFirstname4" runat="server" Width="142px"></asp:TextBox> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell CssClass="style8"> &nbsp;</asp:TableCell> <asp:TableCell CssClass="style4"> Position</asp:TableCell> <asp:TableCell> <asp:TextBox ID="tbPosition4" runat="server" Width="295px"></asp:TextBox> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell CssClass="style8"> &nbsp;</asp:TableCell> <asp:TableCell CssClass="style4"> Department</asp:TableCell> <asp:TableCell> <asp:TextBox ID="tbDept4" runat="server" CssClass="style2" Width="252px"></asp:TextBox> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell CssClass="style8"> &nbsp;</asp:TableCell> <asp:TableCell CssClass="style4"> Campus</asp:TableCell> <asp:TableCell> <asp:DropDownList ID="ddlCampus4" runat="server" CssClass="style2"> <asp:ListItem>Austin</asp:ListItem> <asp:ListItem>Repat</asp:ListItem> <asp:ListItem>Talbot</asp:ListItem> <asp:ListItem>Other</asp:ListItem> </asp:DropDownList> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell CssClass="style8"> &nbsp;</asp:TableCell> <asp:TableCell CssClass="style4"> Building</asp:TableCell> <asp:TableCell> <asp:TextBox ID="tbBuilding4" runat="server" CssClass="style2" Width="307px"></asp:TextBox> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell CssClass="style8"> &nbsp;</asp:TableCell> <asp:TableCell CssClass="style4"> Floor</asp:TableCell> <asp:TableCell> <asp:TextBox ID="tbFlr4" runat="server" CssClass="style2"></asp:TextBox> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell CssClass="style8"> &nbsp;</asp:TableCell> <asp:TableCell CssClass="style4"> Contact No.</asp:TableCell> <asp:TableCell> <asp:TextBox ID="tbTelPager4" runat="server" CssClass="style2" Width="186px"></asp:TextBox> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell CssClass="style8"> &nbsp;</asp:TableCell> <asp:TableCell CssClass="style4"> <asp:Label ID="lblAsset4" runat="server" Text="Asset No. of old device"></asp:Label> </asp:TableCell> <asp:TableCell> <asp:TextBox ID="tbAsset4" runat="server" Width="144px"></asp:TextBox> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell CssClass="style9"> &nbsp;</asp:TableCell> <asp:TableCell CssClass="style12"> Additional comments</asp:TableCell> <asp:TableCell> <asp:TextBox ID="tbComments4" runat="server" Width="340px" Rows="4" TextMode="MultiLine"></asp:TextBox> </asp:TableCell> </asp:TableRow> </asp:Table> <table style="width: 46%;"> <tr> <td> &nbsp;</td> </tr> </table> </asp:Panel> </div>


推荐答案

所以 tb.Text 没有或零长度(阅读:空)。您需要考虑这种可能性在code。

So tb.Text is Nothing or length of zero (read: empty). You need to account for this possibility in your code.

无论哪种,增加一个检查该条件,像这样的:

Either, add a check for that condition, like this:

If Not IsNothing(tb) AndAlso _
   !String.IsNullOrEmpty(tb.Text) AndAlso _
   chkTxt(tb.Text) = False Then

注:使用 AndAlso运算将其他条件短路评估,如果第一个条件是

Note: The use AndAlso will short-circuit the evaluation of the other conditions if the first condition is False.

更改 chkTxt 功能逻辑,使其正确处理没有或空(长度的字符串值零),并返回为条件,像这样的:

Change the logic in the chkTxt function so that it properly handles a string value of Nothing or empty (length of zero) and returns False for that condition, like this:

Public Function chkTxt(text As String) As Boolean
    If String.IsNullOrEmpty(text) Then
        Return False
    End If

    ' Put normal check text logic here
End Function

这篇关于你调用的对象是空的。 vb.net对于一些但不是所有code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆