Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to make a soap request and assign values to productlist which accepts product class as a parameter

[System.Xml.Serialization.XmlArrayItemAttribute("PRODUCT", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public product[] PRODUCTLIST {
    get {
        return this.pRODUCTLISTField;
    }
    set {
        this.pRODUCTLISTField = value;
        this.RaisePropertyChanged("PRODUCTLIST");
    }
}




I am trying to set the values in this way but it is not assigning the values

sup.PRODUCTLIST[0] = new Supcancel.product
                {
                    Name = "Mouse",
                    Price = 20,
                    Size = "Small",
                    date = date
                };


the error i am getting is
object reference not set to instance of an object



Need help

Thanks in advance

What I have tried:

i have tride this way also

sup.PRODUCTLIST[0].Name = "Mouse";
               sup.PRODUCTLIST[0].Price = 20;
               sup.PRODUCTLIST[0].Size = "Small";
               sup.PRODUCTLIST[0].date = date;
Posted
Updated 28-Sep-19 15:47pm
v2
Comments
Afzaal Ahmad Zeeshan 27-Sep-19 20:39pm    
What error do you get?
Member 14583379 28-Sep-19 1:20am    
i am getting this error object reference not set to instance of an object
DerekT-P 28-Sep-19 16:15pm    
what is the value of sup.PRODUCTLIST[0] before you do the above assignment? Because that's all you're doing - assigning a new value to the first element in the sup.PRODUCTLIST array. Have you declared the size of that array? Or did you mean this to be a list? In which case you'll need to ADD it to the (defined) list... The "not set to an instance of an object" is referring the left-hand side, not the right!

1 solution

Since you are getting a null reference exception, the solution is simply:
C#
// Create an array of products that it will use.
sup.PRODUCTLIST = new product[10];
Since you would be adding the elements at runtime, I would highly recommend using other generic types that can accommodate new elements without a need of re-allocation on your part, such as List<T>.

The code would become:
C#
// Change Product to product, if you really think naming should be that way. 
public List<Product> PRODUCTLIST {
    get {
        return this.pRODUCTLISTField;
    }
    set {
        this.pRODUCTLISTField = value;
        this.RaisePropertyChanged("PRODUCTLIST");
    }
}
Then, in the code you should do this,
C#
sup.PRODUCTLIST = new List<Product>();
This is necessary in C#, otherwise NullReferenceException will always be raised and your program will crash.

Check out these past posts of mine to learn more on this:
Null reference exception[^]
Problem with 'Null Reference' Error while creating Class instances[^]
how to handle null reference and out of bound exceptions[^]
Object reference not set to and instance of an object[^]
What Is A Null Error in Code Execution[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900