Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 946 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is a NullReferenceException, and how do I fix it?

#1
I have some code and when it executes, it throws a `NullReferenceException`, saying:

>Object reference not set to an instance of an object.

What does this mean, and what can I do to fix this error?
Reply

#2
Adding a case when the class name for entity used in entity framework is same as class name for a web form code-behind file.

Suppose you have a web form Contact.aspx whose codebehind class is Contact and you have an entity name Contact.

Then following code will throw a NullReferenceException when you call context.SaveChanges()

Contact contact = new Contact { Name = "Abhinav"};
var context = new DataContext();
context.Contacts.Add(contact);
context.SaveChanges(); // NullReferenceException at this line

For the sake of completeness DataContext class

public class DataContext : DbContext
{
public DbSet<Contact> Contacts {get; set;}
}

and Contact entity class. Sometimes entity classes are partial classes so that you can extend them in other files too.

public partial class Contact
{
public string Name {get; set;}
}

The error occurs when both the entity and codebehind class are in same namespace.
To fix this, rename the entity class or the codebehind class for Contact.aspx.

**Reason**
I am still not sure about the reason. But whenever any of the entity class will extend System.Web.UI.Page this error occurs.

For discussion have a look at

[To see links please register here]



Reply

#3
Another general case where one might receive this exception involves mocking classes during unit testing. Regardless of the mocking framework being used, you must ensure that all appropriate levels of the class hierarchy are properly mocked. In particular, all properties of `HttpContext` which are referenced by the code under test must be mocked.

See "[NullReferenceException thrown when testing custom AuthorizationAttribute](

[To see links please register here]

; for a somewhat verbose example.
Reply

#4
It means your code used an object reference variable that was set to null (i.e. it did not reference an actual object instance).

To prevent the error, objects that could be null should be tested for null before being used.

if (myvar != null)
{
// Go ahead and use myvar
myvar.property = ...
}
else
{
// Whoops! myvar is null and cannot be used without first
// assigning it to an instance reference
// Attempting to use myvar here will result in NullReferenceException
}
Reply

#5
You are using the object that contains the null value reference. So it's giving a null exception. In the example the string value is null and when checking its length, the exception occurred.

Example:

string value = null;
if (value.Length == 0) // <-- Causes exception
{
Console.WriteLine(value); // <-- Never reached
}

The exception error is:

> Unhandled Exception:
>
> System.NullReferenceException: Object reference not set to an instance
> of an object. at Program.Main()
Reply

#6
A `NullReferenceException` is thrown when we are trying to access Properties of a null object or when a string value becomes empty and we are trying to access string methods.

For example:

1. When a string method of an empty string accessed:

string str = string.Empty;
str.ToLower(); // throw null reference exception

2. When a property of a null object accessed:

Public Class Person {
public string Name { get; set; }
}
Person objPerson;
objPerson.Name /// throw Null refernce Exception
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through