-1

I am a pretty new developer and I am trying to setup a ODS database connection. I am referencing an example from another programmers work that use to work here. Here is the code. It throws an error on after this. statement. I have my work exactly like his if you want to see his just leave comment it's several lines long.

public ODSData_Codename.TrayLabelReferenceRow toDataRow()
    {
        ODSData_Codename.TrayLabelReferenceRow row= null;
        row.LabelName = this.LabelName;
        row.LabelCode = this.LabelCode;
        row.LabelStock = this.LabelStock;

        return row;
    }

2 Answers2

0

Are you getting a NullReferenceException, Object reference not set to an instance of an object?

you're first line makes row point to null. null doesn't have a property of LabelName. You need to instantiate row to an object of class ODSData_Codename.TrayLabelReferenceRow

You should look at this What is a NullReferenceException and how do I fix it?

Community
  • 1
  • 1
Harrison
  • 3,843
  • 7
  • 22
  • 49
  • You will have to pardon me I'm very new but no it's called unknown member error. it does the first instance of the label values then after the this statement it only gives the option such as equal, get hash code etc – CharmingInferno Oct 23 '13 at 19:09
  • @CharmingInferno is `TrayLabelReferenceRow` a member of `ODSData_Codename`? Is this a run-time error or compile error? – Harrison Oct 26 '13 at 01:30
0

Your NullReferenceException is expected since you are clearly not instantiating your object. Please check the class definition, if you have an empty constructor, you could do this: ODSData_Codename.TrayLabelReferenceRow row = new ODSData_Codename.TrayLabelReferenceRow();

Assuming that you have an empty constructor in your class definition, your code could look like this.

public ODSData_Codename.TrayLabelReferenceRow toDataRow(){
ODSData_Codename.TrayLabelReferenceRow row= new ODSData_Codename.TrayLabelReferenceRow();
row.LabelName = this.LabelName;
row.LabelCode = this.LabelCode;
row.LabelStock = this.LabelStock;

return row;
}
Crocodile
  • 13
  • 1
  • 5
  • Now it says unknown constructor at the (); – CharmingInferno Oct 23 '13 at 19:15
  • Then you don't have an empty constructor for the ODSData_Codename.TrayLabelReferenceRow Class. Are you missing a reference? – Eric J. Price Oct 23 '13 at 19:23
  • @Love2Learn I looked all through my former coworkers file and found nothing where he did – CharmingInferno Oct 23 '13 at 19:30
  • What class are you creating this method within? What is the ODSData_Codename.TrayLabelReferenceRow class and where does it live and does the class you're creating this method within reference that class? – Eric J. Price Oct 23 '13 at 19:31
  • @Love2Learn I just looked over it again and he had it collapsed I didn"t realize it till I used ctrl + F. Thank you for you help all – CharmingInferno Oct 23 '13 at 19:34
  • as @Love2Learn suggested, please look at the definition of the ODSData_Codename.TrayLabelReferenceRow class. if you are new to C# development, There are plenty of tutorials online on the subject. – Crocodile Oct 23 '13 at 19:39