Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7064305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:49:05+00:00 2026-05-28T04:49:05+00:00

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

  • 0

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;
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:49:06+00:00Added an answer on May 28, 2026 at 4:49 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I've got the following code shown below to create a dialog using
Okay, that's what I've got: String[] data = null; String[] data2 = null; String[]
Okay, so I've got a strange problem with the following Lua code: function quantizeNumber(i,
I'm checking my code for memory leaks. Everything is okay until I got the
Okay, so we've got this event. Awesome, right? We've also got a timer with
Okay, so I've got a problem - and I'd love to have it fixed.
Okay, I've got my normal app which is in portrait mode. I can force
Okay, so I've got a Postgres database hosted on a VPS at the moment,
Okay, I think I'm just making a stupid mistake here, but I want to
okay so this is probably a soft pitch question for sombody, but I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.