将值分配给类类型变量

时间:2020-08-13 06:40:33

标签: c# flutterwave

我正在集成到flutterwave API以接收付款。我创建了一个模型,该模型需要将值分配给类类型变量,但是每当这样做时,它都会在我的Web应用程序中引发异常: “在emekaet.dll中发生了'System.NullReferenceException类型的异常,但未在用户代码中处理:附加信息:对象引用未设置为对象的实例。”

示例代码如下所示:

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.

2 个答案:

答案 0 :(得分:1)

您尚未初始化客户。因此,您正在尝试将电子邮件设置为空对象。

尝试

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

答案 1 :(得分:1)

需要创建客户类别

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; } = new Customer();
    public Customermization customermization { get; set; } = new Customermization();
}