ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

What is a NullReferenceException and how do I fix it?

user-image
تم إضافة السؤال من قبل مستخدم محذوف‎
تاريخ النشر: 2016/01/13
Sudheer Muhammed
من قبل Sudheer Muhammed , Senior .Net Developer , ADNOC Logistics and Services

It occurs when a particular code tries to access the members of an uninitialized or null object.

You can either:

1.Avoid accessing the member conditionally using a null check prior to the usage.The recommended way

 2. You can handle the exception using a try catch construct.

 3. Ensure object initialization (not possible in every case).

 

Aadil Dedmari
من قبل Aadil Dedmari , IT Administrator & Programmer , Saudi Rubber Products Co.

NullReferenceException arises when you are trying to use a reference and the reference is not initialized. In other words you can say NullReferenceexception occurs when you use a method or property of reference type whose value is Null

مستخدم محذوف‎
من قبل مستخدم محذوف‎

A NullReferenceException occurs when you try to use a method or property of a reference type (C#, Visual Basic) whose value is null. For example, you may have tried to use an object without first using the new keyword (New in Visual Basic), or tried to use an object whose value was set to null. To avoid this, just check the object is null or not before apply any operation on it.

Check for null (Nothing in Visual Basic) before you use a reference type

Use try-catch-finally (Try-Catch-Finally in Visual Basic) to handle the exception

It's better to avoid a NullReferenceException than to handle it after it occurs. A NullReferenceException is often a non-recoverable error.

·         Your app can ignore objects that are null. For example, if your app retrieves and processes records in a database, you might be able to ignore some number of bad records that result in null objects

 

·         You can recover from the exception. For example, a call to a web service that returns a reference type might return null if the connection is lost or the connection times out. You can attempt to reestablish the connection and try the call again.

 

·         You can restore the state of your app to a valid state. For example, you might be performing a multi-step task that requires you to save information to a data store before you call a method that throws a NullReferenceException. If the uninitialized object would corrupt the data record, you can remove the previous data before you close the app.

 

·         You want to report the exception. For example, if the error was caused by a mistake from the user of your app, you can generate a message to help him supply the correct information. You can also log information about the error to help you fix the problem. Some frameworks, like ASP.NET, have a high-level exception handler that captures all errors to that the app never crashes; in that case, logging the exception might be the only way you can know that it occurs.

 

 

 

Mathew Jacob
من قبل Mathew Jacob , Senior Software Engieer , Dubai Technologies

NullReferenceException occured while you are trying to get any  property or call method of an object which has not yet initialized.

In another word, while you are trying to get a property or call method of object which is absolutely NULL. To avoid this, just check the object is null or not before apply any operation on it.

Mohammed Azhar
من قبل Mohammed Azhar , Consultant , Capgemini India Private Limited

The exception that is thrown when there is an attempt to dereference a null object reference. A NullReferenceException exception is thrown when you try to access a member on a type whose value is null.

Gayasuddin Mohammed
من قبل Gayasuddin Mohammed , Advocate , Practicing Law before High Court at Hyderabad

use exception handling. try() and catch() to handle the exception when throws an error with reference to null object. assign the object or variable appropriately in catch block and redirect it to the required block to proceed further in the code. Thanks.

Ahmed Elagamy
من قبل Ahmed Elagamy , Senior Sharepoint & Dot Net Developer , Zak Solutions for Computer Systems

You are trying to use something that is null (or Nothing in VB.NET Or C# ). This means you either set it tonull, or you never set it to anything at all.See this article  >>>

Mohammad Ali
من قبل Mohammad Ali , Quality Engineer (API Manual/Automation) , Souq.com

This means the reference is null, and you cannot access members through a null reference.

You can figure out where which member have the null value and fix that value.

Saleem Wahlan
من قبل Saleem Wahlan , Programmer , FOS Energy LLC

Mostly occurred when trying to convert null object reference to another datatype.

Example:

Convert.ToInt(Session["ABC"].ToString());

 

where Session["ABC"] is null.

 

Fix:

Validate your Session/ViewState/Object , you can use Try Catch to avoid application breakdown

Example:

if(Session["ABC"]!=null)

Convert.ToInt(Session["ABC"].ToString());

 

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟