Troels Thisted
Troels Thisted

Reputation: 76

NullreferenceException - Object Reference is not set to an instance of a object

I get the Exception: Object Reference is not set to an instance of object.

I have a Globalvariables class that stores one specifik string, if the string is called "OK", then I should be able to load the page, but if the string is "Invalid" I shouldn't be able to Load the page.

In my default page, I have the following Page_load method:

protected void Page_Load(object sender, EventArgs e)
{
    var masterPage = Master;

    if (masterPage != null)
    {
        if (GlobalVariables.Data.StartsWith("OK"))
        {
            //Do stuff
        }
        else
        {
            // Do stuff
        }
    }
}

If I write GlobalGlobalVariables.Data == "OK" it works fine, but if I write like the above i get the error.

Upvotes: 0

Views: 1228

Answers (4)

Dustin Kingen
Dustin Kingen

Reputation: 21245

It seems like the value of GlobalVariables.Data is not getting set before the Page_Load or it does not have a default value.

Solution 1: Add a default value.

public static class GlobalVariables
{
    private static string _data;

    public static string Data
    {
        get { return _data ?? "Invalid"; }
        set { _data = value; }
    }
}

Usage:

if(GlobalVariables.Data.Equals("Ok", StringComparison.OrdinalIgnoreCase))
{

}
else
{

}

But it sounds like you want a flag, so why not use an enum?

Solution 2: Enum

public enum DataStatus
{
    Invalid,
    Ok
}

public static class GlobalVariables
{
    public DataStatus DataStatus { get; set; }
}

Usage:

if(GlobalVariables.DataStatus == DataStatus.Ok)
{

}
else
{

}

Upvotes: 1

Jason Loki Smith
Jason Loki Smith

Reputation: 438

You should refrain from using the "StartsWith" Method if the starts with part is your whole string. I would suggest doing something like this rather:

//this is a case-insensitive check of what the value might be

    if(!string.IsNullorEmpty(GlobalVariables.Data))
   {       
      if (GlobalVariables.Data.Equals("ok",StringComparison.OrdinalIgnoreCase))
       {
          //Do stuff
       }
    }

Upvotes: 0

M.S.
M.S.

Reputation: 71

I'm fairly certain that you didn't initialize the global variable "GlobalVariables.Data". The .NET compiler will automatically initialize it to null, which is why you're getting the null reference exception. == is a static method (refer back to the operator overload MSDN doc for more if you're curious), which is why it doesn't throw the exception.

This all being said, it seems more like you might want to be using a Boolean, not a string, in this case, but I'm not familiar with your use cases.

Upvotes: 1

Renatas M.
Renatas M.

Reputation: 11820

Try to change code a bit

if (!string.IsNullOrWhiteSpace(GlobalVariables.Data) && GlobalVariables.Data.StartsWith("OK"))
{
    //Do stuff
}
else
{
    // Do stuff
}

First check if your string is assigned to something ant then is it starts with OK symbols.

Upvotes: 0

Related Questions