locked
How do I catch this null reference RRS feed

  • Question

  • User-1106011126 posted

    A null reference exception was unhandled by the user's code.

    Below is the bit of code that I wrote to redirect back to my original page.

    Public Class _Default
        Inherits System.Web.UI.Page
        Dim xroot As String = "C:\USERS\EPSNEW\DOCUMENTS\Website34 - Copy (2) \"
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim userid As Int32, rec As String = ConcatA(" ", 78) & Chr(13) & Chr(10), root As String = "C:\users\epsnew\documents\website34 - Copy (2)\", filea As String
            filea = root + "sdata"
            'ChDir(filea)
            'FileClose(3)
            'FileOpen(3, filea, OpenMode.Binar, OpenAccess.ReadWrite, OpenShare.Default, 80)
            If Request.QueryString.HasKeys() Then
                LOGINSTATUS.Text = Request.QueryString("A")         'Here is where the null reference exception is flagged
                userid = LOGINSTATUS.Text
                '       methinks that we need to fetch the other items on screen since we are fully restoring the page
            End If

        End Sub
     

    My question is why the previous statement "If Request.QueryString.HasKeys() Then",
            does not suffice to catch a null reference..   At the point that this occurs, I do not believe that the reference is null.  I guess I do not understand the

    statement.

    Friday, October 3, 2014 12:30 PM

Answers

  • User-1106011126 posted

    Thank you much for a detailed answer.  I am sure that the problem lies in one place or the other, I think the object will be the answer.  When first introducing the code, VS objected to my textbox.TEXT reference and proposed introducing the Textbox object.  I accepted and all seemed well, but I am relatively certain that therein lies the confusion.  Now, I will have to find out how to get around this problem, or resort to a crude messaging system of my own that will allow me to keep code in its own folders and prevent the ambiguity that I have created in sharing the same folder. 

    I am not ungrateful for the knowledge you have provided me in the passing of controls, but its just that there are too many background issues that arise that require me to learn much more than I know now.

    eps123

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 4, 2014 9:45 AM
  • User-1106011126 posted

    I believe you are absolutely right. That will be the problem as another person pointed out.  I'll paste the response to him below.

    Thank you much for a detailed answer.  I am sure that the problem lies in one place or the other, I think the object will be the answer.  When first introducing the code, VS objected to my textbox.TEXT reference and proposed introducing the Textbox object.  I accepted and all seemed well, but I am relatively certain that therein lies the confusion.  Now, I will have to find out how to get around this problem, or resort to a crude messaging system of my own that will allow me to keep code in its own folders and prevent the ambiguity that I have created in sharing the same folder. 

    I am not ungrateful for the knowledge you have provided me in the passing of controls, but its just that there are too many background issues that arise that require me to learn much more than I know now.

    eps123

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 4, 2014 9:57 AM

All replies

  • User753101303 posted

    Hi,

    What if you try LOGINSTATUS.Text="Hello" instead? Does it still throw a null reference exception? I suspect that LOGINSTATUS could be actually the null reference you are looking for.

    Friday, October 3, 2014 12:44 PM
  • User281315223 posted

    You might want to check to ensure both the QueryString exists and that your particular key exists : 

    ' Ensure both your QueryString collection exists and that your "A" key is not empty '
    If Request.QueryString IsNot Nothing AndAlso Not String.IsNullOrEmpty(Request.QueryString("A")) Then
         ' Since it exists, set it '
         LOGINSTATUS.Text = Request.QueryString("A")         
         userid = LOGINSTATUS.Text
    End If
    

    However, if the QueryString itself isn't causing the issue, you might want to look into your LOGINSTATUS object to ensure that it actually exists (and isn't null) as referencing the Text property of it would trigger a null reference as well :

    ' Ensure both your QueryString collection exists and that your "A" key is not empty '
    If Request.QueryString IsNot Nothing AndAlso Not String.IsNullOrEmpty(Request.QueryString("A")) Then
         ' Since it exists, set it '
    
         ' Ensure your LOGINSTATUS element exists ' 
         If LOGINSTATUS IsNot Nothing Then
               LOGINSTATUS.Text = Request.QueryString("A")         
               userid = LOGINSTATUS.Text
         Else
               ' If this executes your LOGINSTATUS element was not found '
         End If
    End If

    You might try using the code above along with a few breakpoints and check which paths are executed.

    Friday, October 3, 2014 12:58 PM
  • User-1106011126 posted

    Thank you much for a detailed answer.  I am sure that the problem lies in one place or the other, I think the object will be the answer.  When first introducing the code, VS objected to my textbox.TEXT reference and proposed introducing the Textbox object.  I accepted and all seemed well, but I am relatively certain that therein lies the confusion.  Now, I will have to find out how to get around this problem, or resort to a crude messaging system of my own that will allow me to keep code in its own folders and prevent the ambiguity that I have created in sharing the same folder. 

    I am not ungrateful for the knowledge you have provided me in the passing of controls, but its just that there are too many background issues that arise that require me to learn much more than I know now.

    eps123

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 4, 2014 9:45 AM
  • User-1106011126 posted

    I believe you are absolutely right. That will be the problem as another person pointed out.  I'll paste the response to him below.

    Thank you much for a detailed answer.  I am sure that the problem lies in one place or the other, I think the object will be the answer.  When first introducing the code, VS objected to my textbox.TEXT reference and proposed introducing the Textbox object.  I accepted and all seemed well, but I am relatively certain that therein lies the confusion.  Now, I will have to find out how to get around this problem, or resort to a crude messaging system of my own that will allow me to keep code in its own folders and prevent the ambiguity that I have created in sharing the same folder. 

    I am not ungrateful for the knowledge you have provided me in the passing of controls, but its just that there are too many background issues that arise that require me to learn much more than I know now.

    eps123

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 4, 2014 9:57 AM