0

I'm getting ‍Object reference not set to an instance of an object in following code

hbprint = await _vw_BPS.FindAsync(x => x.Id == id);

foreach (var hbs in hbprint.Vw_hcsp)          
{   
    var stock = await _vw_hcsp.FindAsync(x=> x.HId == id);
          
    var svm= new Vw_hcsp
    {
        HId = stock.HId,
        HBLContainerId = stock.HBLContainerId,
        Chassis_No = stock.Chassis_No,
        ContainerNo = stock.ContainerNo,
        Seal_No = stock.Seal_No,
        Vehicle_Model = stock.Vehicle_Model,
        Gross_Weight_Stock = stock.Gross_Weight_Stock,
        Volume_Stock = stock.Volume_Stock
     };
}
Mohammad Aghazadeh
  • 2,108
  • 3
  • 9
  • 20
  • Hii Samiksha, Does your code have any value in stock after the FindAsync() methods execution? – Thomas Raj Sep 22 '22 at 12:16
  • No, when control goes to hbprint.Vw_hcsp it is showing me that exception. – Samiksha Bhoir Sep 23 '22 at 09:41
  • That means either hbprint after this line hbprint = await _vw_BPS.FindAsync(x => x.Id == id); is null that is there is no hbprint for the id which your passing. otherwise hbprint doesn't have a property Vw_hcsp.Debugging would help you to find the reason. – Thomas Raj Sep 23 '22 at 12:07
  • hbprint has property Vw_hcsp. – Samiksha Bhoir Sep 24 '22 at 05:39
  • Does it hold any value? Can you tell me the tool(Visual studio/VS Code) you are using ? – Thomas Raj Sep 25 '22 at 00:30
  • Vw_hcsp is a model class and have used it as Icollection in vw_BP model class. There are values in database but it is showing null values here . I am using Visual studio. – Samiksha Bhoir Sep 26 '22 at 05:19
  • As you are using Visual studio, place a break point at this line hbprint = await _vw_BPS.FindAsync(x => x.Id == id); add a dummy line of code like var a = hbprint. press F10 Now hover over hbprint, expand it and share the screenshot here – Thomas Raj Sep 26 '22 at 05:55
  • When I used List model = new List(); it's not showing me that error. I placed breakpoint there but it is showing me empty list. – Samiksha Bhoir Sep 27 '22 at 12:46
  • Yes it will show an empty list only because you are creating an empty list with this line List model = new List();.could you please keep a break point here "hbprint = await _vw_BPS.FindAsync(x => x.Id == id);" and share me the data in that via a screenshot. We can proceed to next step after this step. – Thomas Raj Sep 28 '22 at 03:55

0 Answers0