简体   繁体   English

使用system.datetime的编码Webtest中的空引用

[英]null reference in coded webtest with system.datetime

I was very excited to try out the new feature in VS 2012 ultimate that allows you to run whole load tests with coded web tests. 我非常兴奋地尝试VS 2012 Ultimate中的新功能,该功能使您可以运行带有编码Web测试的整个负载测试。

Unfortunatly, I've ran into a bit of a problem. 不幸的是,我遇到了一个问题。 While trying to debug a web test I've generated (and edited afterwards), I keep getting an NullReferenceException on a simple declaration line. 在尝试调试已生成(然后进行了编辑)的Web测试时,我一直在简单的声明行上得到NullReferenceException I simply cannot wrap my head around why this occurs. 我根本无法确定为什么会发生这种情况。 Here's the code: 这是代码:

[Priority(0)]
public class CreateSessionCoded : WebTest
{
    public string[] SessionID;
    public string[] SessionTime;
    public string[] CreatedTime;

    public CreateSessionCoded()
    {
        this.Context.Add("Parameter1", "");
        this.PreAuthenticate = true;
    }

    public override IEnumerator<WebTestRequest> GetRequestEnumerator()
    {

        //string CurrentITR = this.Context.WebTestIteration.ToString();
        SessionID[this.Context.WebTestIteration] = Guid.NewGuid().ToString();
        SessionTime[this.Context.WebTestIteration] = System.DateTime.UtcNow.ToString();
        CreatedTime[this.Context.WebTestIteration] = System.DateTime.Now.ToString();

    ...

The code goes on, but the part where I get the NRE is at the last two lines where I try to assign values to my SessionTime and CreatedTime parameters. 代码继续进行,但是获得NRE的部分是在最后两行中,我尝试为SessionTimeCreatedTime参数分配值。

It doesn't happen when assigning to the SessionID , so it's not about the WebTestIteration in any way. 分配给SessionID时不会发生,因此与WebTestIteration It also happens if I try to assign a different string (any casual string such as, say, "blabla") to the same parameters. 如果我尝试为相同的参数分配不同的字符串(任何随意的字符串,例如“ blabla”),也会发生这种情况。

I'd really appreciate any help! 我真的很感谢您的帮助! Thanks in advance! 提前致谢! :) :)

You define three arrays: 您定义三个数组:

public string[] SessionID;
public string[] SessionTime;
public string[] CreatedTime;

But, they aren't initialized before you attempt to use them. 但是,在尝试使用它们之前,它们不会被初始化。

Basically, you are doing this: 基本上,您正在这样做:

string[] foo;
foo[1] = "bar";

And you need to do this: 您需要这样做:

string[] foo = new string[10]; // sized appropriately
foo[1] = "bar";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM