Click here to Skip to main content
15,877,754 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void validateName_Click(object sender, EventArgs e)
        {
            if (nameObj.InitializeDataFiles() == mdNAMEOBJECTLib.ProgramStatus.NoError)
            {               
                try
                {
                    File = new StreamReader("../../mdName.sdf");
                }
                catch (Exception)
                {
                    resultString.Text = "Can't open sample file";
                }               
                
                while ((FullName = File.ReadLine()) != null)
                {
                    nameObj.ClearProperties();
                    nameObj.FullName = FullName;
                    nameObj.Parse();
                    fullName.Text = FullName;
                    Records++;
                }
            }
        }


I got error message:Unhandle exception of type 'System.NullReferenceExceiption' occured in mdNameObj.exe.
Additional Information: Object reference not set an instance of an object.

How to fix this error. Thanks,
Posted
Updated 12-Dec-11 9:41am
v3
Comments
Monjurul Habib 12-Dec-11 15:41pm    
provide more detail info.
Sergey Alexandrovich Kryukov 12-Jan-12 17:31pm    
"Unhandle Reference Exception" is gibberish. Exception handling sucks. Why assigning this resultString -- no file name, nothing, wrong place to handle. You need to let exception to propagate. Non-local name FullName in loop suck -- it should be local. Use the debugger.
--SA

1 solution

Use the debugger to find out which object is null, and fix your mistake (you didn't indcate what line of code was throwing the excveption, so I assume you haven't used the debugger yet). The most likely culprits are nameObj and File (which sucks as a variable name since it's also the name of a .Net nameSpace).
 
Share this answer
 
v2
Comments
chaupha 12-Dec-11 16:25pm    
while ((FullName = File.ReadLine()) != null)

The debugger pointed out the error in this line. Sorry, I didn't post.
#realJSOP 12-Dec-11 17:18pm    
so, which variable is null? FullName or File?
chaupha 12-Dec-11 17:51pm    
The FullName is null.
[no name] 12-Dec-11 20:18pm    
5!

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