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

[Solved] Add new record: Object reference not set to an instance of an object

2 Answers 281 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
sebrojas
Top achievements
Rank 1
sebrojas asked on 19 Jul 2010, 05:53 PM

I got this error when I try to add a new record to my grid, at the final is the source code

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 41:     <h2>Administración de Grupos</h2>
Line 42: 
Line 43: <% Html.Telerik().Grid(Model)Line 44:             .Name("Grid")
Line 45:             .DataKeys(keys =>

Source File: c:\Desarrollo\srojas\codigo\Maga\Maga\MagaSeguridadUI\Views\Grupos\Index.aspx    Line: 43

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   Telerik.Web.Mvc.UI.Grid`1.CreateBuilderFor(GridRow`1 row) +1742
   Telerik.Web.Mvc.UI.GridHtmlBuilder`1.CreateRow(GridRow`1 row) +101
   Telerik.Web.Mvc.UI.GridHtmlBuilder`1.AppendRows(IHtmlNode body, IEnumerable`1 dataSource) +331
   Telerik.Web.Mvc.UI.GridHtmlBuilder`1.CreateBody() +572
   Telerik.Web.Mvc.UI.GridHtmlBuilder`1.BuildCore() +594
   Telerik.Web.Mvc.UI.HtmlBuilderBase.Build() +72
   Telerik.Web.Mvc.UI.Grid`1.WriteHtml(HtmlTextWriter writer) +1134
   Telerik.Web.Mvc.UI.ViewComponentBase.Render() +147
   Telerik.Web.Mvc.UI.ViewComponentBuilderBase`2.Render() +55
   ASP.views_grupos_index_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) in c:\Desarrollo\srojas\codigo\Maga\Maga\MagaSeguridadUI\Views\Grupos\Index.aspx:43
   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 c:\Desarrollo\srojas\codigo\Maga\Maga\MagaSeguridadUI\Views\Shared\Site.Master:30
   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) +56
   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


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1

View:
<% Html.Telerik().Grid(Model)
            .Name("Grid")
            .DataKeys(keys =>
            {
                keys.Add(g => g.GrupoID);
            })
            .ToolBar(commands => commands.Insert())
            .Columns((IEnumerable<GridColumnSettings>)ViewData["Columns"])
            .DataBinding(dataBinding => dataBinding.Server()
                .Select("Index", "Grupos")
                .Insert("Crear", "Grupos")
                .Update("Editar", "Grupos")
                .Delete("Eliminar", "Grupos"))
            .CellAction(cell =>
            {
                // "DataItem" is the Order object to which the current row is bound to
                if (cell.Column.Title == "Estado" && cell.DataItem.Estado != null)
                {
                    //Set the background of the entire row                        
                    if (cell.DataItem.Estado.EstadoID == 2)
                        cell.HtmlAttributes["style"] = "color:#CC0000;";
                    else
                        cell.HtmlAttributes["style"] = "color:green;";
                }
            })
            .DetailView(perfilesDetailView => perfilesDetailView.Template(g =>
            {
                %>
                    <% Html.Telerik().Grid(g.Perfiles)
                            .Name("Periles_" + g.GrupoID)
                            .DataKeys(keys =>
                            {
                                keys.Add(p => p.PerfilID);
                            })
                            .Columns(columns =>
                            {
                                columns.Bound(p => p.Aplicacion.Nombre);
                                columns.Bound(p => p.Estado);
                                columns.Bound(p => p.Nombre);
                                columns.Bound(p => p.Descripcion);
                            })
                            .CellAction(cell =>
                            {
                                // "DataItem" is the Order object to which the current row is bound to
                                if (cell.Column.Title == "Estado" && cell.DataItem.Estado != null)
                                {
                                    //Set the background of the entire row                        
                                    if (cell.DataItem.Estado.EstadoID == 2)
                                        cell.HtmlAttributes["style"] = "color:#CC0000;";
                                    else
                                        cell.HtmlAttributes["style"] = "color:green;";
                                }
                            })
                            .Pageable(settings => settings.Style(GridPagerStyles.NextPreviousAndNumeric))
                            .Sortable()
                            .Render();
                    %>
                <%
            }
            ))
            .RowAction(row =>
            {
                if (row.Index == 1)
                {
                    row.DetailRow.Expanded = true;
                }
            })
            .Pageable(settings => settings.Style(GridPagerStyles.NextPreviousAndNumeric))
            .Sortable()
            .Filterable()
            .Groupable()
            .Editable(editing => editing.Mode(GridEditMode.InLine))
            .Resizable(resizing => resizing.Columns(true))
            .Render();
    %>

Controller:

public ActionResult Index()
        {
            var columns = new[]             
            {                
                new GridColumnSettings                
                {                    
                    Member = "Aplicacion",                    
                    Width = "130px"                
                },                
                new GridColumnSettings                
                {                    
                    Member = "Estado",                    
                    Width = "130px",                 
                },                
                new GridColumnSettings                
                {                    
                    Member = "Nombre",                    
                    Width = "130px"                
                },                
                new GridColumnSettings                
                {                    
                    Member = "Descripcion",                    
                    Width = "300px"                
                },                
                new GridCommandColumnSettings                
                {                    
                    Commands =                     
                    {                        
                        new GridEditActionCommand(),                        
                        new GridDeleteActionCommand()                    
                    },
                    Width = "180px",
                    Title = "Acciones"
                }
            };
            ViewData["Columns"] = columns;
            ViewData["aplicaciones"] = serviciosGrupos.getAplicaciones();
            ViewData["estados"] = serviciosGrupos.getEstados();
            return View(serviciosGrupos.getGrupos());
        }

Thank you

2 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 20 Jul 2010, 07:21 AM
Hi sebrojas,

Unfortunately the stacktrace does not indicate what may have caused this problem. Could you please provide a sample project? Since the forum does not allow attachments you can use some public hosting web site such as dropbox.com or skydrive.live.com.

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
Harvey
Top achievements
Rank 1
answered on 23 Jul 2010, 04:16 AM
I had a similar problem when clicking the "add new record" button for insert.
I found that the data model object the grid was displaying needed a constructor defined which initialised all of the public get/set properties. This solved my issue.
Tags
Grid
Asked by
sebrojas
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Harvey
Top achievements
Rank 1
Share this question
or