1. Zhang JX
  2. SnapDevelop
  3. Monday, 6 June 2022 04:38 AM UTC

Hi all,

I am trying to work on API service for our ERP system Invenotry function, need make use of nested Json structure to pass values:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace NDYAPI
{
    public class StockAdj
    {
        public string doc_format = "YYMM";
        public string coy_code;
        public string doc_type;
        public string status_code = "P";
        public string requester_code;
        public string requester_name;
        public DateTime requested_date;
        public string invoice_code = "";
        public int doi_num = 0;
        public string remarks;
        public string reason_code;
        public int item_count; 
        public StockAdjitem[] adjitems;        
        
    }
    public class StockAdjitem
    {
        public int item_num;
        public string trans_type;
        public string prod_code;
        public string prod_to;
        public string part_code;
        public string part_to;

    }

}

While incurring "Exception Thrown" with "System.NullReferenceException: Object reference not set to an instance of an object." when setting the value to nested sub items.

am i missed something with the class defination ?

Thank you.

Zhang

 

Logan Liu @Appeon Accepted Answer Pending Moderation
  1. Monday, 6 June 2022 07:41 AM UTC
  2. SnapDevelop
  3. # 1

Hi JX,

Please make sure you have created stockAdj.adjitems so this field is not NULL.  E.g.: stockAdj.adjitems = new StockAdjitem[Items.Count];

You also have to create each array item before accessing its field. E.g.: stockAdj.adjitems[stockAdjItemNum] = new StockAdjitem();

Alternatively, you can directly use the item as your adjitems[] array element if the logic is corrrect. E.g.:

stockAdj.adjitems = new StockAdjitem[Items.Count];

foreach (var item in Items)
{

 // stockAdj.adjitems[stockAdjItemNum] = new StockAdjitem(); // Then you can set values to it's fields
 stockAdj.adjitems[stockAdjItemNum] = item;
 stockAdjItemNum++; // Please notice that it uses zero-based index for array in C#
}

Regards, Logan

Comment
  1. Zhang JX
  2. Monday, 6 June 2022 08:34 AM UTC
Hi Logan,

Thank you very much, it is working now.

Regards,

Zhang
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.