Search code examples
c#flutterwave

Assigning value to a class type variable


I am integrating to flutterwave API to receive payments. I created a model that will require assigning values to a class type variable, but whenever I do this, it throws exception in my web application: "An exception of type 'System.NullReferenceException' occurred in emekaet.dll but was not handled in user code:Additional information: Object reference not set to an instance of an object."

The sample code is as shown below:

public class Customer
{
    public string email { set; get; }
    public string phonenumber { get; set; }
    public string name { get; set; }
}

public class FlutterWaveRequestModel
{
    public string tx_ref { get; set; }
    public long amount { get; set; }
    public string currency { get; set; }
    public string redirect_url { get; set; }
    public string payment_options { get; set; }
    public Meta meta { get; set; }
    public Customer customer { get; set; }
    public Customermization customermization { get; set; }
}

FlutterWaveRequestModel reqModel = new FlutterWaveRequestModel();
reqModel.amount = _Amount * 100;            
reqModel.redirect_url = _CallbackUrl;
reqModel.tx_ref = _Ref;
reqModel.payment_options = "card";
reqModel.customer.email = _Email;  -- error occur at this point.

Solution

  • You have not initialized Customer. So you're trying to set email on an object which is null.

    Try

    reqModel.customer = new Customer();
    reqModel.customer.email = _Email;