locked
Request word empty RRS feed

  • Question

  • User1519602350 posted

    How to handle the below error, If request word is empty. Request["searchTerm"]

    Code:

        var searchWords = Request["searchTerm"].Split(' ');
        IEnumerable<dynamic> result = Enumerable.Empty<string>();
        

    Error:

    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 6:      var db = Database.Open("demo");
    Line 7:  
    Line 8: var searchWords = Request["searchTerm"].Split(' '); Line 9:      IEnumerable<dynamic> result = Enumerable.Empty<string>();
    Monday, July 29, 2013 1:22 PM

Answers

  • User-265225665 posted
    if(Request["searchTerm"] != null)
    {
        var searchWords = Request["searchTerm"].Split(' ');
        IEnumerable<dynamic> result = Enumerable.Empty<string>();
    }
    or 
    
    if( !string.IsNullOrEmpty( Request["searchTerm"].ToString()))
    {
        var searchWords = Request["searchTerm"].Split(' ');
        IEnumerable<dynamic> result = Enumerable.Empty<string>();
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 29, 2013 1:29 PM
  • User281315223 posted

    It is alwasy important to perform null-checking on collections such as the Request object to ensure a value actually exists prior to attempting to access it to avoid null reference exceptions : 

    //Check if your "searchTerm" key exists in the Request
    if(Request["searchTerm"] != null)
    {
        //It is not null, so you can attempt to access it's value
        var searchWords = Request["searchTerm"].Split(' ');
        IEnumerable<dynamic> result = Enumerable.Empty<string>();
    }
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 29, 2013 1:47 PM

All replies

  • User-265225665 posted
    if(Request["searchTerm"] != null)
    {
        var searchWords = Request["searchTerm"].Split(' ');
        IEnumerable<dynamic> result = Enumerable.Empty<string>();
    }
    or 
    
    if( !string.IsNullOrEmpty( Request["searchTerm"].ToString()))
    {
        var searchWords = Request["searchTerm"].Split(' ');
        IEnumerable<dynamic> result = Enumerable.Empty<string>();
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 29, 2013 1:29 PM
  • User281315223 posted

    It is alwasy important to perform null-checking on collections such as the Request object to ensure a value actually exists prior to attempting to access it to avoid null reference exceptions : 

    //Check if your "searchTerm" key exists in the Request
    if(Request["searchTerm"] != null)
    {
        //It is not null, so you can attempt to access it's value
        var searchWords = Request["searchTerm"].Split(' ');
        IEnumerable<dynamic> result = Enumerable.Empty<string>();
    }
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 29, 2013 1:47 PM