This is a migrated thread and some comments may be shown as answers.

TimePicker null reference exeception after postback

13 Answers 237 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
quynh
Top achievements
Rank 1
quynh asked on 26 Mar 2011, 12:33 AM
I recently upgraded to version 2011.1.315 and noticed my TimePicker throws Object reference not set to an instance of an object after a postback.

At first i was using TimePickerFor() after i start getting the errors i changed it to TimePicker(). but still gets the error. i am using the timepicker within a telerik().Window(). 
<%= Html.Telerik().TimePicker().Name("startTime").ClientEvents(events => { events.OnChange("eventStartTimeChange"); }).HtmlAttributes(new { Style = "width:110px" })%>

here is the error message after postback. Any ideas?

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 96:                         <%: Html.ValidationMessageFor(model => model.eventDate)%></div></td></tr>
Line 97:                     <tr><td>Event Hours</td>
Line 98:                         <td>From:&nbsp;<%= Html.Telerik().TimePicker().Value(DateTime.Now).ClientEvents(events => { events.OnChange("eventStartTimeChange"); }).HtmlAttributes(new { Style = "width:110px" })%>
Line 99:                         &nbsp;&nbsp;&nbsp;To:&nbsp;<%= Html.Telerik().TimePicker().Name("endTime").HtmlAttributes(new { Style = "width:110px" })%><br />

Source File: MVC\Views\Event\EventDetail.aspx    Line: 98

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   Telerik.Web.Mvc.UI.InputComponentExtensions.GetAttemptedValue(IInputComponent`1 instance) +121
   Telerik.Web.Mvc.UI.Html.TimePickerHtmlBuilder.InputTag() +151
   Telerik.Web.Mvc.UI.Html.TimePickerHtmlBuilder.Build() +304
   Telerik.Web.Mvc.UI.TimePicker.WriteHtml(HtmlTextWriter writer) +190
   Telerik.Web.Mvc.UI.ViewComponentBase.ToHtmlString() +121
   Telerik.Web.Mvc.UI.ViewComponentBuilderBase`2.ToHtmlString() +56
   Telerik.Web.Mvc.UI.ViewComponentBuilderBase`2.ToString() +30
   System.Web.HttpWriter.Write(Object obj) +27
   System.Web.Mvc.SwitchWriter.Write(Object value) +15
   System.Web.UI.HtmlTextWriter.Write(Object value) +37
   ASP.<>c__DisplayClass5.<__RenderContent2>b__0() in \Views\Event\EventDetail.aspx:98
   Telerik.Web.Mvc.UI.HtmlTemplate.<set_Content>b__9(Object ) +39
   Telerik.Web.Mvc.UI.<>c__DisplayClass3.<set_CodeBlockTemplate>b__2(TextWriter writer) +56
   Telerik.Web.Mvc.UI.HtmlElement.WriteTo(TextWriter output) +195
   Telerik.Web.Mvc.UI.<>c__DisplayClass5.<WriteTo>b__3(IHtmlNode child) +40
   Telerik.Web.Mvc.Extensions.EnumerableExtensions.Each(IEnumerable`1 instance, Action`1 action) +200
   Telerik.Web.Mvc.UI.HtmlElement.WriteTo(TextWriter output) +315
   Telerik.Web.Mvc.UI.Window.WriteHtml(HtmlTextWriter writer) +365
   Telerik.Web.Mvc.UI.ViewComponentBase.Render() +131
   Telerik.Web.Mvc.UI.ViewComponentBuilderBase`2.Render() +55
   ASP.views_event_onsiteeventnewdetail_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) in \Views\Event\EventDetail.aspx:66
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in \Shared\Site.Master:31
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +43
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060


13 Answers, 1 is accepted

Sort by
0
quynh
Top achievements
Rank 1
answered on 26 Mar 2011, 05:08 PM
this happens on both timepicker and datepicker.  once i remove the where the error occurs, the next timepicker errors out. 

here is the original code:
<%: Html.Telerik().TimePickerFor(model => model.startTime).ClientEvents(events => events.OnChange("eventStartTimeChange")).HtmlAttributes(new { Style = "width:110px" })%>

changed to: 
<%= Html.Telerik().TimePicker().Name("startTime").ClientEvents(events => { events.OnChange("eventStartTimeChange"); }).HtmlAttributes(new { Style = "width:110px" })%>

both above ok on initial load but errors after postback.  i thought it was due to my view model property being a nullable datetime field but why would it throw an error even when i use timepicker()?
0
Georgi Krustev
Telerik team
answered on 28 Mar 2011, 09:21 AM
Hello quynh,

Could you please try to reproduce the depicted issue on our online demo?
In the demo  DatePicker, TimePicker and DateTimePicker components are bound to DateTime? properties.

Kind regards,
Georgi Krustev
the Telerik team
0
quynh
Top achievements
Rank 1
answered on 28 Mar 2011, 04:16 PM
Hello Georgi, I wasn't able to reproduce the error on the demo. i'm not sure how this even works before the upgrade.  i was able to do a workaround this problem. 

I have 2 forms on my strongly type view. 1 form is on the main view and the other form is on my telerik().window(). my date/time picker is on my telerik window form

what i tried that works is i added another timepicker with same name in my main view form. set that to hidden. that fixed it but sort of a hack

EDIT: the work around does not work for me.  i went back to the backup version 2010.3.1318 and my page renders ok after the delete postback.  i also changed the 2011.1.315 version code to a non-nullable datetime property and it works fine.
0
Travis
Top achievements
Rank 1
answered on 11 Aug 2011, 11:55 PM
I'm getting the same error, althought it may not be related.  I'm building a multi-step form which has a field that is using the datepicker on step one.  All of my steps are in partial views.  I changed the "Date.cshtml" EditorTemplate to be for a nullable DateTime since that is the type of my field.  Here is the new Date.cshtml editor template:
@model DateTime?    
@(Html.Telerik().DatePicker()
  .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))
  .HtmlAttributes(new { id = ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty) + "_wrapper" })
  )

Everything works fine in Step 1, but if I go sto Step 2 and then go back to Step 1 I get the exact error mentioned earlier in this post.  Any idea why this might be?  Also I noticed that if I add a prefix to the .Name property, the error does not occur, but then it also changes the date field name that is posted so as to no longer be a property of the model:
.Name("dp" + ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))
0
Atanas Korchev
Telerik team
answered on 12 Aug 2011, 07:06 AM
Hi Travis,

 Unfortunately this is not a known problem. Is there a chance to provide a sample project which reproduces the null reference exception?

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

0
Kevin
Top achievements
Rank 1
answered on 15 Aug 2011, 11:58 PM
I believe I am having a similar issue.  DateTimePickerFor or TimePickerFor is not working for me in a Grid.  These controls have null values when posted back to the controller.  The DatePickerFor works, but not any of the pickers with Time in it.

Steve
0
Travis
Top achievements
Rank 1
answered on 16 Aug 2011, 12:12 AM
I was able to get the jquery datepicker plugin to work for me so it hasn't been as high a priority.  I'll try and create a test project when I have time.
0
Samuel
Top achievements
Rank 1
answered on 16 Aug 2011, 07:27 PM
Having the same issue. Did you manage to resolve this?
0
Georgi Krustev
Telerik team
answered on 17 Aug 2011, 09:22 AM
Hello,

 
I have created a test project in order to reproduce the problem, but to no avail. It is attached it to this project. Could you please check it and modify it in order to replicate this issue? Thus I will be able to observe it locally and possibly fix the problem.

Best regards,
Georgi Krustev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

0
Ryan
Top achievements
Rank 2
answered on 24 Aug 2011, 06:46 PM
We are experiencing the same issue running Telerik MVC version 2011.1.622.  Here is our scenario.

We have a form to Create a Task object with a hidden DateTimePicker for CompletionDate (hidden through jquery).
The task can be In Progress or Completed. 
    IF the user selects completed, jQuery is used to display the CompletionDate DateTimePicker.
    If the user selects in progress, the CompletionDate DateTimePIcker is left invisible.
The task is validated to see if it can be saved during the POST operation.
If valid and no errors occur, the user is directed back to a list of tasks.
If invalid or an error occurs then model.ErrorMessage is set and the return View(model) occurs.
If the user had set Completed and set a CompletionDate, all is well and the POST version of the page renders fine with the error message.
However, if the user set In Progress and the CompletionDate DateTimePicker was NOT visible at the time of the POST, this error occurs in the exact same way as the original poster. 

Summary:  If the DateTimePicker is hidden during the POST, the error occurs upon return View(model);


0
Ryan
Top achievements
Rank 2
answered on 24 Aug 2011, 07:28 PM
To follow up, the workaround above where you manually set the DateTimePicker control name seems to be working for me.  You have to manually handle mapping it to a property on your model though, which sucks, but at least I can move on.
0
Nick
Top achievements
Rank 1
answered on 03 Oct 2011, 08:40 PM
I think I am having the same issue described in this post.

To reproduce:
1) Put DatePickerFor in a partial view and bind to a nullable DateTime property. This property should have a [Required] data annotation.
2) Put a Telerik TabStrip in a view, and have one tab (not the first tab) LoadContentFrom the partial view containing the DatePickerFor.

Whether the nullable DateTime property has a value or not, the object reference error will show up when the tab containing the partial view is clicked. Making the property a non-nullable DateTime fixes the issue in my case.


I modified your project to show the error.
0
Georgi Krustev
Telerik team
answered on 07 Oct 2011, 10:52 AM
Hi Nick,

Thank you for the updated demo.

I am glad to inform you that this already fixed and the fix is available with Q2 SP1 2011. 

Kind regards,
Georgi Krustev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
Tags
Date/Time Pickers
Asked by
quynh
Top achievements
Rank 1
Answers by
quynh
Top achievements
Rank 1
Georgi Krustev
Telerik team
Travis
Top achievements
Rank 1
Atanas Korchev
Telerik team
Kevin
Top achievements
Rank 1
Samuel
Top achievements
Rank 1
Ryan
Top achievements
Rank 2
Nick
Top achievements
Rank 1
Share this question
or