object-reference-not-set-to-an-instance-of-an-object in Editor version 2.0.4 but not 1.9.6
object-reference-not-set-to-an-instance-of-an-object in Editor version 2.0.4 but not 1.9.6
I upgraded my Editor license from 1.9 to 2.0 and I am having an issue with an "object-reference-not-set-to-an-instance-of-an-object " error only when using 2.0.4. When using editor 1.9.6 the code below works adding, editing, and deleting from a SQL Server database. After I upgraded to 2.04, the code allows editing and deleting existing but does not allow adding new records. The error is coming from the Controller at the "return Json(response);" line.
Thank you in advance.
Randy
My controller example code:
using CapEx.Models;
using DataTables;
using Microsoft.AspNetCore.Mvc;
using System;
namespace CapEx.Controllers
{
public class CapExController : Controller
{
[Route("api/capex")]
[HttpGet]
[HttpPost]
public ActionResult CapEx()
{
var dbType = Environment.GetEnvironmentVariable("DBTYPE");
var dbConnection = Environment.GetEnvironmentVariable("DBCONNECTION");
using var db = new Database(dbType, dbConnection);
var response = new Editor(db, "Project", "Project.Id")
.Model<CapExProjectModel>()
.Field(new Field("Project.Status")
.Options(new Options()
.Table("Status")
.Value("Id")
.Label("Name")
)
.Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
)
.Field(new Field("Project.Region")
.Options(new Options()
.Table("Region")
.Value("Id")
.Label("Name")
)
.Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
)
.Field(new Field("Project.Functional_Area")
.Options(new Options()
.Table("Functional_Area")
.Value("Id")
.Label("Name")
)
.Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
)
.Field(new Field("Project.Building")
.Options(new Options()
.Table("Building")
.Value("Id")
.Label("Name")
)
.Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
)
.Field(new Field("Project.Budget_Year")
.Options(new Options()
.Table("Budget_Year")
.Value("Id")
.Label("Name")
)
.Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
)
.Field(new Field("Project.Currency")
.Options(new Options()
.Table("Currency")
.Value("Id")
.Label("Name")
)
.Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
)
.Field(new Field("Project.Project_Type")
.Options(new Options()
.Table("Project_Type")
.Value("Id")
.Label("Name")
)
.Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
)
.Field(new Field("Project.Description")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "Description Required!" }))
.Validator(Validation.MinMaxLen(5, 85, new ValidationOpts { Message = "Description must be between 5 and 85 charactors!" }))
)
.Field(new Field("Project.Budget_Amount")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "Budget Amount Required!" }))
)
.LeftJoin("Status", "Status.Id", "=", "Project.Status")
.LeftJoin("Region", "Region.Id", "=", "Project.Region")
.LeftJoin("Functional_Area", "Functional_Area.Id", "=", "Project.Functional_Area")
.LeftJoin("Building", "Building.Id", "=", "Project.Building")
.LeftJoin("Budget_Year", "Budget_Year.Id", "=", "Project.Budget_Year")
.LeftJoin("Currency", "Currency.Id", "=", "Project.Currency")
.LeftJoin("Project_Type", "Project_Type.Id", "=", "Project.Project_Type")
.TryCatch(false)
.Process(Request)
.Data();
return Json(response);
}
}
}
Model Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CapEx.Models
{
public class CapExProjectModel
{
public class Project
{
public int Id { get; set; }
public int CapExId { get; set; }
public string CapExNumber { get; set; }
public string Description { get; set; }
public int Status { get; set; }
public int Region { get; set; }
public int Functional_Area { get; set; }
public int Building { get; set; }
public int Budget_Year { get; set; }
public string Project_Leader { get; set; }
public string MAR_Number { get; set; }
public string WBS_Number { get; set; }
public decimal Budget_Amount { get; set; }
public int Currency { get; set; }
public int Project_Type { get; set; }
}
public class Status
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Region
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Functional_Area
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Building
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Budget_Year
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Currency
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Project_Type
{
public int Id { get; set; }
public string Name { get; set; }
}
}
}
This question has an accepted answers - jump to answer
Answers
Thanks for your question. Which version of 1.9.x was it you upgraded from? Also, are you using .NET Core or .NET Framework?
Could you add
.Debug(true)
just before your.Process(...)
line and see if that helps the server return valid JSON?Thanks,
Allan
Thank you for the quick turn around. I upgraded from .NET Core using Editor 1.9.6 and upgraded to 2.04. I added the
.Debug(true)
and I and still getting the error.I did a little investigation and found that if I upgrade from 1.9.6 to 2.0.0 or 2.0.1 I can still add projects to the SQL database. When I tried upgrading from 2.0.1 to 2.0.2, I received the same error.
My JavaScript is below. Maybe you can see something that is incorrect for version 2.0.2 and above.
Many thanks for your investigation into this!
The only significant change for v2.0.2 in the .NET dll was this one, but I'm not sure why it would be giving you an error and not us.
Does the error message include a backtrace at all?
Allan
Hopefully the StackTrace can help you. Thank you for your help.
I might have it, could you add:
to your C# Editor field list please? By default fields used from the model are read / write, but this one you don't want to write (no value is submitted from the client-side for it), setting it not writeable will allow the database to compute the value for it (assuming it is a serial).
Allan
Thank you Allen! I added the line to my controller and works perfect.
Would the fix be the same case for any read-only field or just a column that has Identity specified? An example would be a column in SQL Server generated by a trigger event (i.e. Date_Created)?
I think I answered my own question by running a few simple tests. The only field that is throwing the error is the Id field that is read-only on the server-side. My fields that have triggers are not throwing any errors as they are still writable.
Allen, thanks again for the help!
Glad we got to the bottom of it! Yes, I would say that any field you don't want as writeable should have
.Set(false)
- even if you don't list it in your client-side code, if anyone happened to guess the name of the field, they could attempt to write to it by messing around with the Ajax edit request.Allan
I am having the same issue. I used .Set(false) on my fields and I still get the issue.
@appliedvisions Can you post your Editor code please so we can take a look,
Colin
set the id field to false fixed my problem. i spent 4 hours for the bug until i read the post. Thanks allan!!