0

Please I need some help with this. I have seen similar question here but without any answer marked correct. I have used json2csharp to get the classes for a json string which I want to use to post to an API.

Here's the code:

 public class AccountDetails
    {
        public string ProductCode { get; set; }
        public string AccountClass { get; set; }
        public string AccountSide { get; set; }
        public string AccountType { get; set; }
        public bool TaxableAccount { get; set; }
        public string AccountSubjectToInterestCalculation { get; set; }
        public string FrequencyOfDebitInterestCalculation { get; set; }
        public string FrequencyOfCreditInterestCalculation { get; set; }
        public string CodeForInterestLadderPrinting { get; set; }
        public int AccountSubjectToDeductionAtSource { get; set; }
        public string ServiceCode { get; set; }
        public string AccountStatementCode { get; set; }
        public string BranchCode { get; set; }
        public string CurrencyCode { get; set; }
        public string CustomerID { get; set; }
    }

    public class AddressDetails
    {
        public string City { get; set; }
        public string CountryCode { get; set; }
        public string PoBox { get; set; }
        public string PostalCode { get; set; }
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public string AddressLine3 { get; set; }
        public string AddressType { get; set; }
        public string County { get; set; }
        public string BranchCode { get; set; }
        public string EmailAddress { get; set; }
    }

    public class PhoneDetails
    {
        public string PhoneType { get; set; }
        public string PhoneNumber { get; set; }
    }

    public class EmailDetails
    {
        public string EmailType { get; set; }
        public string EmailAddress { get; set; }
    }

    public class CustomerDetails
    {
        public string CustomerType { get; set; }
        public string TitleCode { get; set; }
        public string BVN { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public string FullName { get; set; }
        public string Gender { get; set; }
        public string FamilyStatusCode { get; set; }
        public string DoB { get; set; }
        public string BirthCity { get; set; }
        public string BirthCounty { get; set; }
        public string BirthRegion { get; set; }
        public string IdentificationNumber { get; set; }
        public string NationalityCode { get; set; }
        public string ResidentialCountryCode { get; set; }
        public string AccountOfficer { get; set; }
        public string SectorIdentifier { get; set; }
        public string SectorCode { get; set; }
        public string ProfileCode { get; set; }
        public string Signature { get; set; }
        public string Photo { get; set; }
        public AddressDetails AddressDetails { get; set; }
        public PhoneDetails PhoneDetails { get; set; }
        public EmailDetails EmailDetails { get; set; }
    }

    public class RootObject
    {
        public string RequestID { get; set; }
        public AccountDetails AccountDetails { get; set; }
        public CustomerDetails CustomerDetails { get; set; }

        public PhoneDetails PhoneDetails { get; set; }

        public EmailDetails EmailDetails { get; set; }

        public AddressDetails AddressDetails { get; set; }
    }

And I am doing this to create the object, pass values and post...

 var jsonObj = new RootObject();

            jsonObj.CustomerDetails.FirstName = fname;
            jsonObj.CustomerDetails.LastName = lastnamebox.Text;
            jsonObj.CustomerDetails.PhoneDetails.PhoneNumber = phonebox.Text;
            jsonObj.CustomerDetails.AddressDetails.AddressLine1 = addressbox.Text;
            jsonObj.CustomerDetails.DoB = dobbox.Text;
            jsonObj.CustomerDetails.EmailDetails.EmailAddress = emailbox.Text;

but... There's a problem with this code, FirstName, LastName etc, always have the error object reference not set to an instance of an object, which kind of makes sense I believe; due to the classes.

Can someone kindly assist me with how I can get the classes fixed or if theres a way to create the instances. I hope I'm making sense... Thanks in anticipation.

noetico
  • 25
  • 6
  • You instantiated `jsonObj`, but not `CustomerDetails`, which is still null. You can either do so after creating `jsonObj`, or in the `RootObject` constructor. –  Feb 16 '20 at 01:02
  • Look up on how to initialize "objects / Classes" within constructors. Hint: inside RootObject, `public RootObject () { AccountDetails = new AccountDetails(); ... }`. Or you can initialize it after `var jobject = new RootObject; jobject.CustomerDetails = new CustomerDetails();` etc.. – Jawad Feb 16 '20 at 01:02

0 Answers0