News_Edit NullReferenceException RRS feed

  • Question

  • User984834470 posted

    I get a NullReferenceException when I run news_edit.aspx that points to this line of code:

    If surl.Text <> "" Then

    Monday, May 7, 2007 11:57 AM

All replies

  • User-820230059 posted

    These exceptions always baffled me, but the best answer is to call them a .NET charatcher behaviour.

    You cant call the .Text or .ToString functions on a string that's null, you can usually resolve this by doing a simple check first.

    If not string.isnullorempty(surl) then


    End If

    Rather than the If surl.text <> "" then


    End if


    Monday, May 7, 2007 12:08 PM
  • User984834470 posted

    I replaced the code as you suggested and it generated another error:

    Error 1 Value of type 'System.Web.UI.WebControls.TextBox' cannot be converted to 'String'. 

    Here is the context:


    Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)

    If FormView1.CurrentMode = FormViewMode.Insert Then

    CType(FormView1.FindControl("dtpicker"), DateandTimePicker).selectedDateTime = Now


    Dim cb As CheckBox = CType(FormView1.FindControl("CheckBox1"), CheckBox)

    Dim surl As TextBox = CType(FormView1.FindControl("staticURLTextBox"), TextBox)

    'If surl.Text <> "" Then

    If Not String.IsNullOrEmpty(surl) Then

    cb.Checked = True

    surl.Enabled = True


    surl.Enabled = False

    End If


    End If

    End Sub


    Monday, May 7, 2007 2:08 PM
  • User-820230059 posted

    It appears the surl is a textbox, not a string like I'd originally thought.  I dont have the project, so it gets a little harder to diagnose.

    The object reference error is probably happening because it's not finding the control on the page during this statment....

    Dim surl As TextBox = CType(FormView1.FindControl("staticURLTextBox"), TextBox)

    Your best bet, is to turn tracing on - and take a look at the hierchy of the controls leading up to staticURLTextBox and put the entire control name in there.

    You can also remove the code I provided, and put back the default code, since if it's finding the textbox correctly, it should work fine.

    Tuesday, May 8, 2007 10:17 AM