为 class 类型变量赋值

Assigning value to a class type variable

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

示例代码如下:

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.

您还没有初始化客户。所以你试图在一个空的对象上设置电子邮件。

尝试

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

需要创建客户class

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