C# 为类类型变量赋值

C# 为类类型变量赋值,c#,flutterwave,C#,Flutterwave,我正在集成到flatterwaveAPI以接收付款。我创建了一个需要为类类型变量赋值的模型,但每当我这样做时,它就会在我的web应用程序中引发异常: “emekaet.dll中发生类型为'System.NullReferenceException'的异常,但未在用户代码中处理:其他信息:对象引用未设置为对象的实例。” 示例代码如下所示: public class Customer { public string email { set; get; } public string

我正在集成到
flatterwave
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.

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

试一试


需要创建一个customer类

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();
}

尝试在项目中的
reqModel.payment\u options
处设置断点,并调试代码直至该断点。您应该看到有关未初始化的对象的信息。我的猜测是,您需要先创建
customer
对象,然后才能向其设置电子邮件。@Hayden您是对的,
flatterWaveRequestModel
上的
customer
属性未初始化,因此它将导致NullPointerException快速响应。现在可以了。谢谢。
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();
}