skip to Main Content

I ‘m developing an application which will search for a product & retrieve all details like sellerInfo,shippingInfo,price,etc…However ,I’m getting error “Object reference not set to an instance of an object” while retrieving seller username & shippingInfo with the following syntax: This is for shipping cost:

 SearchItem[] items = response.searchResult.item;
double shippingcost=items[i].shippingInfo.shippingServiceCost.Value.

This is for sellerInfo:

string sellerInfo = items[i].sellerInfo.sellerUserName;

Please help,The control is going directly to catch block & raising exception. I tried checking for the null using if condition but no success its raising exception.

2

Answers


  1. Chosen as BEST ANSWER

    I solved this myself.What I did is I created an Object of shipping info like the above:

    string selleruname; 
    double shippingcost;
    SellerInfo si = new SellerInfo();
    ShippingInfo ship = new ShippingInfo();
    SearchItem[] items = response.searchResult.item;
    
    
    
    
                        si = items[i].sellerInfo;
                        if (si == null)
                        {
                           selleruname = "Not Present";
                        }
                        else
                        {
                             selleruname=si.sellerUserName;
                        }
                        ship = items[i].shippingInfo;
                        if (ship.shippingServiceCost == null)
                        {
                            shippingcost = 0.0;
    
                        }
                        else
                        {
                            shippingcost = ship.shippingServiceCost.Value;
                        }
    

    Thanks to all for reply & your efforts.

    Regards Rishiraj M Shengule [email protected]


  2. Please Check the null Values as:

    if(somval!=null && somval!=DBNULL.Value)
    {
      ///do stuff
    }
    

    Hope this helps you!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search