Anicho
Anicho

Reputation: 2667

Why will it always be a null value?

Okay I got the following class, but item is always null when I GetIstance(); Visual studio shows:

Field 'PageStyle.item' is never assigned to, and will always have its default value null

How can I resolve this? What am I doing wrong? Is there a better way of doing whats done below?

public class PageStyle
{

private static PageStyle _Instance = null;

// Instantiate variables relating to sitecore item paths.
Database webDB;
Sitecore.Data.Items.Item item;

// constructor
private PageStyle()
{
    if (webDB != null)
    {
        webDB = Sitecore.Configuration.Factory.GetDatabase("web");
        Sitecore.Data.Items.Item item = webDB.Items[StartItem];
    }
}

// Method that gets instance
public static PageStyle GetInstance()   
{       
    if (_Instance == null)          
    _Instance = new PageStyle();        
    return _Instance;   
}

private void InitializeWebDB()
{
   if (webDB == null)
   {
    webDB = Sitecore.Configuration.Factory.GetDatabase("web");
   }

}

private void InitializeStartItem()
{
    if (webDB != null)
    {
        item = webDB.Items[StartItem];
    }
}

public string StartItem
{
    get
    {
        return _startItem;
    }

    set
    {
        _startItem = value;
    }
}
}

Upvotes: 1

Views: 581

Answers (5)

x-silencer
x-silencer

Reputation: 13

Perhaps placing the webDB init method outside of the class and making it static would not hurt. In your code, you are checking webDB against null twice as well as in the GetDatabase() method.

You could code this part like this:

public class PageStyle
{
    private static PageStyle _Instance;

    // Instantiate variables relating to sitecore item paths.
    private static readonly Database webDB = DbInitializer.InitializeWebDb("web");
    ...

Upvotes: 0

RobJohnson
RobJohnson

Reputation: 885

Your not initialising it in the constructor, your creating a new local variable with the same name, use:

// constructor
private PageStyle()
{
    webDB = Sitecore.Configuration.Factory.GetDatabase("web");
    this.item = webDB.Items[StartItem];
}

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Maybe you meant to assign the item in the constructor:

private PageStyle()
{
    if (webDB != null)
    {
        webDB = Sitecore.Configuration.Factory.GetDatabase("web");
        this.item = webDB.Items[StartItem];
    }
}

Also make sure that you call the InitializeWebDB private method somewhere or the webDB variable will also be null.

Like this:

private PageStyle()
{
    InitializeWebDB();

    if (webDB != null)
    {
        this.item = webDB.Items[StartItem];
    }
}

Upvotes: 2

Anders Marzi Tornblad
Anders Marzi Tornblad

Reputation: 19305

You never set it to a value. You might think that you do, but you really only ever set a local variable with the same name. Also, you're checking the webDB variable for non-null values at a time when it is always null:

// constructor
private PageStyle()
{
    if (webDB != null)
    {
        webDB = Sitecore.Configuration.Factory.GetDatabase("web");
        Sitecore.Data.Items.Item item = webDB.Items[StartItem];
    }
}

Change this to:

// constructor
private PageStyle()
{
    this.webDB = Sitecore.Configuration.Factory.GetDatabase("web");
    this.item = webDB.Items[StartItem];
}

I'm assuming that you always need a database instance, and that your if (webDB != null) was a mistake.

Upvotes: 3

Ian Horwill
Ian Horwill

Reputation: 3025

You have a local instance of item in the constructor, shadowing the class level field.

Upvotes: 0

Related Questions