Why is this object reference supposedly not set to an instance of an object that has obviously been identified by the compiler?

岁酱吖の 提交于 2019-12-13 09:39:15

问题


Some people -- perhaps gullibly -- believe that there are multiple ways of being a catskinner.

When my attempt to search all the Controls on a Page for Checkboxes continued to fail ignominiously (for proof of that, see this), I thought maybe I could just look at the ID of the Control, rather than care what type of control it was at heart.

So, I commented out this line:

If TypeOf cntrl Is System.Web.UI.WebControls.CheckBox Then

...and tried this instead:

If cntrl.ID.ToString().Contains("ckbx")

But I fared no better, and received this coldwater slap:

Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

Line 69:             LabelDebug.Text = LabelDebug.Text+" "+cntrl.GetType().ToString+" "
Line 70:             'If TypeOf cntrl Is System.Web.UI.WebControls.CheckBox Then
Line 71:             If cntrl.ID.ToString().Contains("ckbx")
Line 72:             'Dim objAsConvertible As IConvertible = TryCast(cntrl, IConvertible)
Line 73:             'If objAsConvertible Is Nothing Then 

Source File: C:\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_categoryadmin.aspx.vb    Line: 71 

Stack Trace: 

[NullReferenceException: Object reference not set to an instance of an object.]
   pages_custmaint_categoryadmin.Button1_Click(Object sender, EventArgs e) in C:\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_categoryadmin.aspx.vb:71
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +114
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +139
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +28
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2980

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5491

Why is it that the control, which obviously exists, throws an exception as if there is no reference to it?

The entire block of code is:

Dim coName As String
Dim argVals(2) As String
Dim _Unit As String
Dim _MemberNo As String
Dim _CustNo As String
Dim curCheckboxIDVal As String

For Each cntrl As Control In Me.Controls
    'If TypeOf cntrl Is System.Web.UI.WebControls.CheckBox Then
    If cntrl.ID.ToString().Contains("ckbx")
        If DirectCast(cntrl, CheckBox).Checked = True Then
            Label2.Text = "label 2 text from checked"
            curCheckboxIDVal = CStr(DirectCast(cntrl, CheckBox).ID)
            coName = GetLabelTextForID(curCheckboxIDVal)
            argVals = GetArgValsForCompanyName(coName)
            _Unit = argVals(0)
            _MemberNo = argVals(1)
            _CustNo = argVals(2)
            Label2.Text = _Unit
            LabelDebug.Text = _MemberNo
            Using conn As New SqlConnection(connStr), _
                cmd As New SqlCommand(upd8DML, conn)
                cmd.Parameters.Add("@Unit", SqlDbType.VarChar, 50).Value = _Unit
                cmd.Parameters.Add("@MemberNo", SqlDbType.VarChar, 50).Value = _MemberNo
                cmd.Parameters.Add("@CustNo", SqlDbType.VarChar, 50).Value = _CustNo
                conn.Open()
                cmd.ExecuteScalar()
            End Using
        End If
    End If
Next

To get to the line that's throwing the error, cntrl has to be a valid Control in the Me.Controls collection; I reckon all Controls have an ID property. So why that err is being thrown on that line makes little, if any, sense to me.


回答1:


the problem is in here:

If cntrl.ID.ToString().Contains("ckbx")  

cntrl.ID seems to be null. Try to debug to see what value you have in your cntrl.ID.ToString()

Checking if value is Null:

If String.IsNullOrEmpty(cntrl.ID) then exit For

or

 If cntrl.ID Is Nothing Then exit For



回答2:


The fix ended up being simple, and even logical, in hindsight.

The controls are dynamically added to the form, like so:

formCustCatMaint.Controls.Add(coName) And so, replacing this line, in the loop:

For Each cntrl As Control In Me.Controls ...with this:

For Each cntrl As Control In formCustCatMaint.Controls And this line, in the GetLabelTextForID() function:

For Each cntrl As Control In Me.Controls ...with this:

For Each cntrl As Control In formCustCatMaint.Controls ...did the trick. The controls are being found, and the code is working as designed/originally expected.

Nebenbei bemerkt, this also works fine now:

If TypeOf cntrl Is CheckBox Then


来源:https://stackoverflow.com/questions/42963456/why-is-this-object-reference-supposedly-not-set-to-an-instance-of-an-object-that

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!