ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to read appSettings JSON from Class Library in ASP.NET Core
  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

Handling Error - Object Reference Not Set To An Instance Of Object

.Net ASP.NET about 7 years ago || 11/25/2018 || 894 View

  • Hide

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

  • 0
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Manish Pandit

Manish Pandit

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey