Click here to Skip to main content
15,860,861 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
hello plz help i found some problem with code behind (Object reference not set to an instance of an object.) i think there is some null value but i cant find it there is the code

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        FileUpload newfileuploadcontrol = (FileUpload)GridView1.FindControl("FileUpload1") as
           FileUpload;
        TextBox newTextbox = (TextBox)GridView1.FindControl("TextBox1") as TextBox;
        

        if (newfileuploadcontrol.HasFile) ----------------> problem is here  
        {

            newfileuploadcontrol.SaveAs(Server.MapPath("~/Cars/Images/") + newfileuploadcontrol.FileName);

            newTextbox.Text = "~/Cars/Images/" + newfileuploadcontrol.FileName;


        }
    }


What I have tried:

i have tried to find null value but i didn't find it
Posted
Updated 7-Mar-17 12:19pm
v2
Comments
member60 8-Mar-17 3:14am    
can you please add more details of fileUpload in gridview, I mean the code snipet of gridview to understand where you added the control in grid.
F-ES Sitecore 8-Mar-17 4:44am    
This is a problem you are going to find on a regular basis so it is vital you learn to debug your code. You can't simply post your code on a thread and have strangers guess what the problem might be, when you can debug your code to find out for sure what the problem actually is. Use breakpoints and stepping through the code along with the analysis of variable contents and evaluating expressions to track where the nulls are coming from.

https://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-Beginn

1 solution

since the newfileuploadcontrol is null, and trying to access any of its properties or methods will result in null reference exception

validate for null values before accessing the properties

 
if(newfileuploadcontrol!= null) // validate null value
if (newfileuploadcontrol.HasFile)


Note: this is just to by pass the null reference error, but you will only have to find the actual cause for null values in the object which you expect something.
 
Share this answer
 
v2
Comments
Member 13044689 7-Mar-17 18:30pm    
thank u sir
Karthik_Mahalingam 7-Mar-17 19:02pm    
welcome :)

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