Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How To Solve Error For Object reference not set to an instance of an object.
Posted
Comments
Rahul VB 29-Jan-14 1:32am    
Firstly,
When you get an exception and the code breaks into the debugger, try hovering your mouse over all the objects which have been used in that particular line, something might be null. Try to reinitialize that object in a proper way.

Secondly,
Kindly post some code.

Check this http://tutorials.csharp-online.net/index.php?title=CSharp_FAQ:_What_does_Object_reference_not_set_to_an_instance_of_an_object_mean[^]

Sample:

C#
class Program
{


    static void Main(string[] args)
    {
        AnyClass objAnyClas = null;
        objAnyClas.AnyProperty = 25; // Object reference error raises , since the object is null
        objAnyClas.AnyMethod();      // Object reference error raises  , since the object is null

        // to avoid the error you can check for null values of the object
        if (objAnyClas != null)
        {
            objAnyClas.AnyMethod();
            var some = objAnyClas.AnyProperty;
        }


    }

    class AnyClass
    {
        public int AnyProperty { get; set; }
        public void AnyMethod() { }
    }
}


Added:Check the link in Tadit Dash solution.
 
Share this answer
 
v2
Comments
Rahul VB 29-Jan-14 1:30am    
I was just about to post the same thing.Very well said.
Karthik_Mahalingam 29-Jan-14 1:33am    
Thanks Rahul VB :)
Rahul VB 29-Jan-14 1:39am    
haha :) what i wanted to say was that you read my mind sometimes :))
Karthik_Mahalingam 29-Jan-14 1:42am    
oh :)
Refer the most popular answer by Sergey ( @SAKryukov ) "Object Reference not set to Instance of an object", please see the code below.[^].
 
Share this answer
 
v2
Comments
Joezer BH 29-Jan-14 11:45am    
5ed!
Thanks Canny. :)
Hai
i think ur connectoin not properly or if u read some data form databse,in that data not retrive form databsae,if u post ur code and show me which line u get that error.

In most cases,connection property not set ,may be u not specify the db path .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900