Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is correct way to check for Null exception?

Tags:

c#

asp.net

Which is the most correct code?

if (HttpContext.Current.Response.Cookies[authCookieName] != null) {
    HttpContext.Current.Response.Cookies[authCookieName].Value = "New Value";
}

or

if (HttpContext.Current != null)
    if (HttpContext.Current.Response != null)
        if (HttpContext.Current.Response.Cookies != null)
            if (HttpContext.Current.Response.Cookies[authCookieName] != null)
                HttpContext.Current.Response.Cookies[authCookieName].Value = "New Value";
like image 743
Stephen Price Avatar asked Oct 03 '08 02:10

Stephen Price


People also ask

How do I find NullPointerException?

NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

How do you write a null check in Java?

Use “==” to check a variable's value. If you set a variable to null with “=” then checking that the variable is equal to null would return true. variableName == null; You can also use “!= ” to check that a value is NOT equal.

Does isEmpty check for null?

isEmpty(< string >)​ Checks if the <string> value is an empty string containing no characters or whitespace. Returns true if the string is null or empty.


2 Answers

If any one of HttpContext, HttpContext.Current, HttpContext.Current.Response, or Http.Current.Response.Cookies is null, you're already in trouble. Let the exception happen and fix your web server.

like image 137
yfeldblum Avatar answered Oct 05 '22 06:10

yfeldblum


Both are good. Assuming that you have already checked everything else that need to be checked first. E.g.:

private bool CheckSuspendersAndBelt()
{
    try
    {
        //ensure that true is true...
        if (true == true)
        {
            //...and that false is false...
            if (false == false)
            {
                //...and that true and false are not equal...
                if (false != true)
                {
                    //don't proceed if we don't have at least one processor
                    if (System.Environment.ProcessorCount > 0)
                    {
                        //and if there is no system directory then something is wrong
                        if (System.Environment.SystemDirectory != null)
                        {
                            //hopefully the code is running under some version of the CLR...
                            if (System.Environment.Version != null)
                            {
                                //we don't want to proceed if we're not in a process...
                                if (System.Diagnostics.Process.GetCurrentProcess() != null)
                                {
                                    //and code running without a thread would not be good...
                                    if (System.Threading.Thread.CurrentThread != null)
                                    {
                                        //finally, make sure instantiating an object really results in an object...
                                        if (typeof(System.Object) == (new System.Object()).GetType())
                                        {
                                            //good to go
                                            return true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    }
    catch
    {
        return false;
    }
}

(sorry, couldn't resist... :) )

like image 35
KristoferA Avatar answered Oct 05 '22 07:10

KristoferA