Handling Error - Object Reference Not Set To An Instance Of Object
It is the error that occur when developing a new web application. It occurs due to mistake that occur during coding.It is mainly faced by new developers. It is difficult to understand for a beginner. This error description gives message that an object that is being called to get or set its value has no reference. It means that we are trying to access an object that was not initilized. This error occurs when an null(empty) value is passed. On passing null value during run time, an exception is thrown.
Consequences of object Reference Error
- An exception is thrown that leads program not to run.
- This kind of error will lead to security issues which make the sensitive data available for the hackers .This kind of exposure are mainly used by hackers to steal data and take down server.
There are many cases where the programmer forgets to initilize an object before implementing it.
The Code is shown below for the Exception
How to avoid exposing code and entities?
We must always remove code that could possibly throw an exception inside try catch blocks. There are others security approaches which we can use to protect our data that can be found.
Code In Controller
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ExceptionHandling.Models;
namespace ExceptionHandling.Controllers
{
public class HomeController : Controller
{
Myobj myobj;
MyNextObject mynextobject;
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()
{
mynextobject.MyNextObjMember = "error";
return View();
}
public IActionResult ConditionStatement()
{
if (true == false)
{
myobj = new Myobj();
myobj.MyobjMember = "";
}
else
mynextobject.MyNextObjMember = "error";
return View();
}
public IActionResult ObjectInsideObject()
{
myobj = new Myobj();
myobj.myNextObject.MyNextObjMember = "error";
//we get exception because our child object is not intilize
return View();
}
public IActionResult AddInNullList()
{
lstSample.Add("error");
return View();
}
}
}
In Model following code is
using System;
namespace ExceptionHandling.Models
{
public class ErrorViewModel
{
public string RequestId { get; set; }
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
public class Myobj
{
public string MyobjMember { get; set; }
public MyNextObject myNextObject { get; set; }
}
public class MyNextObject
{
public string MyNextObjMember { get; set; }
}
}
New object not instantiated
Example
public IActionResult NewObject()
{
mynextobject.MyNextObjMember = "error";
return View();
}
Condition statement(if, switch)
public IActionResult ConditionStatement()
{
if (true == false)
{
myobj = new Myobj();
myobj.MyobjMember = "";
}
else
mynextobject.MyNextObjMember = "error";
return View();
}
Object Inside Object
Example
public IActionResult ObjectInsideObject()
{
myobj = new Myobj();
myobj.myNextObject.MyNextObjMember = "error";
//we get exception because our child object is not intilize
return View();
}
Add item in a null list
public IActionResult AddInNullList()
{
lstSample.Add("error");
return View();
}
Output of new object not initilized
Screenshot of NullList
Screenshot of Conditional Statement
Screenshot of Object Inside Object