跟踪NullReferenceException

时间:2016-04-13 01:19:24

标签: c# webmatrix asp.net-webpages

这是一个简单的Web UI。允许用户创建新标记的表单。它昨天与Validation合作,但今天似乎停止了。它现在在某个点抛出NullReferenceException。注释掉引用会将异常移回文件中。

有缺陷的位

const string sDateTimeFormat = "dd-MM-yyyy HH:mm:ss";
DateTime dtStartPeriod = DateTime.MinValue;
DateTime dtEndPeriod = DateTime.MinValue;
DateTime createDate = DateTime.Now;
string cellNumber1 = null;
string cellNumber2 = null;
string cellNumber3 = null;

if (int.TryParse(Request.Form["vehicleId"], out value))
{
    vehicleId = value;
}
else
{
    vehicleId = -1;
}

Validation.Add("vehicleId",
    Validator.Integer("You must choose a valid vehicle"),
    Validator.Required("You must choose a vehicle from the list"),
    Validator.Range(1000, 9999, "Pick a vehicle from the list provided")
);

Validation.Add("startDate",
    Validator.DateTime(),
    Validator.Required("Start date is a required field"),
    Validator.StringLength(19, 19, "Start date's format is " + sDateTimeFormat + " and is 19 characters long")
);

Validation.Add("endDate",
    Validator.DateTime(),
    Validator.Required("End date is a required field"),
    Validator.StringLength(19, 19, "End date's format is " + sDateTimeFormat + " and is 19 characters long")
);

Html表单元素

        <fieldset name="startDateFieldSet">
            <label for="startDateControl">Pick Start Date</label>
            <input type="datetime"
                   name="startDate"
                   id="startDateControl"
                   class="@Validation.ClassFor("startDate")"
                   value="@((dtStartPeriod == DateTime.MinValue) ? "" : dtStartPeriod.ToString(sDateTimeFormat))" /> 

            @Html.ValidationMessage("startDate")
            <br /><span class="requiredFormat">@sDateTimeFormat</span>
        </fieldset>

        <fieldset name="endDateFieldSet">
            <label for="endDateControl">Pick End Date</label>
            <input type="datetime"
                   name="endDate"
                   id="endDateControl"
                   class="@Validation.ClassFor("endDate")"
                   value="@((dtEndPeriod == DateTime.MinValue) ? "" : dtEndPeriod.ToString(sDateTimeFormat))" />

            @Html.ValidationMessage("endDate")
            <br /><span class="requiredFormat">@sDateTimeFormat</span>
        </fieldset>

截图 screenshot 1 http://www.ctrackonline.com.au/screenshots/anchor-1.jpg
screenshot 2 http://www.ctrackonline.com.au/screenshots/anchor-2.jpg
screenshot 3 http://www.ctrackonline.com.au/screenshots/anchor-3.jpg
screenshot 4 http://www.ctrackonline.com.au/screenshots/anchor-4.jpg

2 个答案:

答案 0 :(得分:1)

是由于你的@ cellNumber1.ToString?

答案 1 :(得分:0)

在“异常”设置中,选中“系统异常”下的NullReferenceException。这将导致它在它出现的第一个地方被抛出。

您可以从Debug - &gt;访问例外窗口。 Windows - &gt; VS 2015中的例外情况