Question

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;
    }
Was it helpful?

Solution

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?

OTHER TIPS

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top