1

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; } } } 

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice, essentially doing it this way I can get rid of Initialize WebDB() method. Cheers buddy will mark as answer if works.
2

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]; } } 

Comments

0

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

Comments

0

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]; } 

2 Comments

That still would never set any values.
True, removed the if statement, although others have already provided a better answer now anyway :-S
0

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"); ... 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.