1

I have the data being pulled from a separate c# file called data. The class for data is Version. I used a separate class to make my Label class. When I compile it, I get the, Object reference not set to an instance of an object. I am not sure why the error is happening. Since it is tied to a database.

public Data Version { get; set; }
    public Label Label { get; set; }

    public Form1()
    {
        InitializeComponent();

        CmboBoxLabel.Items.Add(new Label(Version.LabelName, Version.LabelCode));
    }

If you want any of the class code let me know.

Data Version Class

public class Data
{
    public Data()
    {
        LabelName = "";
        LabelCode = -1;
        LabelStock = -1;

    }

    public string LabelName { get; set; }
    public Int32 LabelCode { get; set; }
    public Int32 LabelStock { get; set; }

    public ODSData_XXXXX.TrayLabelReferenceRow toDataRow()
    {
        ODSData_XXXX.TrayLabelReferenceRow row = null;
        row.LabelName = this.LabelName;
        row.LabelCode = this.LabelCode;
        row.LabelStock = this.LabelStock;
        return row;
    }

    public static Data loadFromDataRow(ODSData_XXXX.TrayLabelReferenceRow row)
    {
        Data Version = new Data();
        Version.LabelName = row.LabelName;
        Version.LabelCode = row.LabelCode;
        Version.LabelStock = row.LabelStock;

        return Version;
    }
}
5
  • 1
    Have you instantiated Version anywhere? Commented Oct 24, 2013 at 15:48
  • @MyCodeSucks Yes in a separate file Commented Oct 24, 2013 at 15:49
  • @MyCodeSucks I will post. Commented Oct 24, 2013 at 15:49
  • 1
    You have to instantiate it in the scope of the Form1() constructor. Or make it a global variable. Commented Oct 24, 2013 at 15:50
  • @MyCodeSucks Wow I figured this stuff would come easier to me. Commented Oct 24, 2013 at 15:54

1 Answer 1

1

You're not instantiating Version anywhere in the scope of where you're wanting to use the variable. Do something like this:

public Label Label;

public Form1()
{
    Data version = new Data(); // this creates and instantiates a new Data object named Version
    InitializeComponent();

    CmboBoxLabel.Items.Add(new Label(version.LabelName, version.LabelCode));
}

Of course, after you create the version, you'll need to populate it's properties however you would like. Ultimately, you'll want to look more into Classes and Properties and how they work.

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

1 Comment

@CharmingInferno: No problem. I hope this helped. If so, please remember to click the check box to accept the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.