Why isn't this object reference supposedly set to an instance of an object that has obviously been identified by the compiler?

Some people - perhaps gullibly - believe that there are several ways to become a feline.

As my attempt to find all the controls on the page for checkboxes kept failing (for proof of this, see this ), I thought maybe I could just look at the ID of the control rather than what type of control it was in the 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 got nothing and got this cold water:

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 does a control that obviously exists throw an exception if there is no reference to it?

The whole block of code:

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 navigate to the line where the error was thrown, cntrl must be a valid control in the Me.Controls collection; I believe all controls have an ID property. So why this mistake is thrown on this line makes little difference to the meaning, if any.

-2


source to share


2 answers


The fix turned out to be simple and even logical in retrospect.

Controls are dynamically added to the form, for example:

formCustCatMaint.Controls.Add (coName) So, replacing this line in a loop:

For each cntrl as control in Me.Controls ... with this:

For each cntrl As control In formCustCatMaint.Controls And this line in GetLabelTextForID () function:



For each cntrl as control in Me.Controls ... with this:

For each cntrl How control In formCustCatMaint.Controls ... did the trick. The controls are found and the code works as designed / originally expected.

Nebenbei bemerkt, now this works great:

If TypeOf cntrl Is CheckBox Then

      

0


source


the problem is here:

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

      

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

Checking for Null value:



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

      

or

 If cntrl.ID Is Nothing Then exit For

      

+1


source







All Articles