Few of our clients sometimes encounter this error message and we decide to write tutorial about this error message and how to fix this issue.
Why does this error happen?
This error’s description speaks for itself but when you do not have much experience in development, it is very difficult to understand. So, this error description says that an object that is being called to get or set its value has no reference. This means that you are trying to access an object that was not instantiated.
Common Mistakes that Cause this Error
You must always wrap code that could possibly throw an exception inside try-catch blocks. There are others security approaches that you can use to protect your data that can be found here.
public class HomeController : Controller { SampleObj sampleObj; SampleChildObj sampleChild; List<string> lstSample; public IActionResult Index() { return View(); } public IActionResult About() { ViewData["Message"] = "Your application description page."; return View(); } public IActionResult Contact() { ViewData["Message"] = "Your contact page."; return View(); } public IActionResult Error() { return View(); } public IActionResult NewObject() { sampleChild.Item2 = "error"; return View(); } public IActionResult ConditionStatement() { if (true == false) { sampleChild = new SampleChildObj(); sampleChild.Item2 = ""; } else sampleChild.Item2 = "error"; return View(); } public IActionResult ObjectInsideObject() { sampleObj = new SampleObj(); sampleObj.ChildObj.Item2 = "error"; return View(); } public IActionResult AddInNullList() { lstSample.Add("error"); return View(); } }
public class SampleObj { public string Item1 { get; set; } public SampleChildObj ChildObj { get; set; } } public class SampleChildObj { public string Item2 { get; set; } }
New object not instantiated
public IActionResult NewObject() { sampleChild.Item2 = "error"; return View(); }
This happens when you create a new object but do not instantiate it before getting/setting a value.
Condition statement(if, switch)
public IActionResult ConditionStatement() { if (true == false) { sampleChild = new SampleChildObj(); sampleChild.Item2 = ""; } else sampleChild.Item2 = "error"; return View(); }
Why does this happen?
Object Inside Object
Here, we have a sample situation of when we have this error:
public IActionResult ObjectInsideObject() { sampleObj = new SampleObj(); sampleObj.ChildObj.Item2 = "error"; return View(); }
Why this happens?
Add item in a null list
public IActionResult AddInNullList() { lstSample.Add("error"); return View(); }
Why does this happen?
- In order to avoid exposing your data, you must always handle exceptions. Read more about how to do that here.
- The items listed above are some of the most common ways to throw this type of error but there are many other situations in which we may face it. Always remember to check if your objects are instantiated before reading or writing data into them.
Javier is Content Specialist and also .NET developer. He writes helpful guides and articles, assist with other marketing and .NET community work