Click here to Skip to main content
16,019,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on mvc 5 project, there is 3 input type file uploader inside view page.

Here if I am selecting the only one fileUploader, another two are passing null. I want to avoid to save if null existing. By using below code, how can I solve that issue?

error: "Object reference not set to an instance of an object."

please find the below image, that showing error cause.
[^]

What I have tried:

C#
[HttpPost]
 public ActionResult PremiumUserRegistration(IEnumerable<HttpPostedFileBase> fileUpload)
      {
 IList<HttpPostedFileBase> list = (IList<HttpPostedFileBase>)fileUpload;
                HttpFileCollectionBase files = Request.Files;
                var httpPostedFile = HttpContext.Request.Files[0];      

if (httpPostedFile.ContentLength != 0)
                    {
                        for (int i = 0; i < fileUpload.Count(); i++)
                        {
                            if (list[i].ContentLength > 0 && i == 0)
                            {
                                HttpPostedFileBase file = files[i];
                                LogoImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName + "_" + list[i].FileName);
                                var path = Path.Combine(Server.MapPath("~/UploadedImages/PremiUserRegistration/LogoImage/"), LogoImage);
                                file.SaveAs(path);
                            }
                            else if (list[i].ContentLength > 0 && i == 1)
                            {
                                HttpPostedFileBase file = files[i];
                                TitlePictureImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName + "_" + list[i].FileName);
                                var path = Path.Combine(Server.MapPath("~/UploadedImages/PremiUserRegistration/TitlePicture/"), TitlePictureImage);
                                file.SaveAs(path);
                            }
                            else if (list[i].ContentLength > 0 && i == 2)
                            {
                                HttpPostedFileBase file = files[i];
                                TitlePictureImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName + "_" + list[i].FileName);
                                var path = Path.Combine(Server.MapPath("~/UploadedImages/PremiUserRegistration/Brochure/"), TitlePictureImage);
                                file.SaveAs(path);
                            }
                        }
                    }
}


Please help me...
Posted
Updated 8-Mar-17 5:30am
v2
Comments
Richard MacCutchan 8-Mar-17 11:28am    
Just check if the variable is null before you try to use it.
Member 12955507 8-Mar-17 11:32am    
Yes, sir variable is null. Because, from 3 file uploader I will use only one or two even all. If I am not using one then how can I manage the code? Please help me
Richard MacCutchan 8-Mar-17 11:42am    
Are you saying that you do not know how to write an if (variable == null) statement?
Rajesh Kumar 2013 8-Mar-17 20:48pm    
yes! sir, I did write if (List[i] != null) now it working fine. Thank you.
Richard Deeming 9-Mar-17 11:29am    
IList<HttpPostedFileBase> list = (IList<HttpPostedFileBase>)fileUpload;

If you want the argument type to be IList<HttpPostedFileBase>, then declare it as such.

As it stands, your method could accept anything that implements IEnumerable<HttpPostedFileBase>, even if it doesn't also implement IList<HttpPostedFileBase>, and your hidden requirement would lead to a runtime exception.

1 solution

add this line to validate null value

for (int i = 0; i < fileUpload.Count(); i++)
            {
                if (List[i] != null)
                {
                    if (list[i].ContentLength > 0 && i == 0)
                    {
                        HttpPostedFileBase file = files[i];
                        LogoImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName + "_" + list[i].FileName);
                        var path = Path.Combine(Server.MapPath("~/UploadedImages/PremiUserRegistration/LogoImage/"), LogoImage);
                        file.SaveAs(path);
                    }
                    else if (list[i].ContentLength > 0 && i == 1)
                    {
                        HttpPostedFileBase file = files[i];
                        TitlePictureImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName + "_" + list[i].FileName);
                        var path = Path.Combine(Server.MapPath("~/UploadedImages/PremiUserRegistration/TitlePicture/"), TitlePictureImage);
                        file.SaveAs(path);
                    }
                    else if (list[i].ContentLength > 0 && i == 2)
                    {
                        HttpPostedFileBase file = files[i];
                        TitlePictureImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName + "_" + list[i].FileName);
                        var path = Path.Combine(Server.MapPath("~/UploadedImages/PremiUserRegistration/Brochure/"), TitlePictureImage);
                        file.SaveAs(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